使用基本 HTML 和 JavaScript 的 WebSQL CRUD 函数
我这里有一个示例数据库,它需要 CRUD 函数来从 UI 添加更多数据,它是一个 WebSQL,我似乎找不到方法,有人需要帮助吗?用户应该能够保存和创建、读取、更新和删除他们的输入数据。
e
<!DOCTYPE html>
<html>
<head>
<title>Contact Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
// Populate the database
//
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Transaction error callback
//
function errorCB(err) {
alert("Error processing SQL: "+err);
}
// Transaction success callback
//
function successCB() {
alert("Database created, and polated");
}
</script>
</head>
<body>
<h1>Example</h1>
<p>SQLTransaction</p>
</body>
</html>
I have an example database here, which needs CRUD functions to add more data from the UI, its a WebSQL and I cant seem to find a way through, anyone wna help? Users should be able to save and create, read, update and delete their input data.
e
<!DOCTYPE html>
<html>
<head>
<title>Contact Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
// Populate the database
//
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Transaction error callback
//
function errorCB(err) {
alert("Error processing SQL: "+err);
}
// Transaction success callback
//
function successCB() {
alert("Database created, and polated");
}
</script>
</head>
<body>
<h1>Example</h1>
<p>SQLTransaction</p>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将数据库逻辑直接放入客户端的 html 中并不安全,因为任何人都可以通过查看源代码来看到它。考虑使用任何服务器端脚本(PHP、ASP.NET)。
It is not secure to place a db-logic directly into client's html as anyone will be able to see it by looking into a source code. Consider using any server-side scripting (PHP, ASP.NET).
Websql 本质上是 CRUD。如果您不了解 SQL,则需要学习它。有一个工具 websql.js 可以将调用包装在 Promise 中以获得更好的语法,但仍然使用很多SQL。
Websql is CRUD by nature. You will need to learn SQL if you do not know it. There is a tool websql.js that wraps calls in promises for a nicer syntax but very much still uses SQL.