开放数据库你好世界
我正在尝试了解 openDatabase,并且我想我正在将其插入到 TABLE1,但我无法验证 SELECT * FROM TABLE1 是否正常工作。
<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript">
var db;
$(function(){
db = openDatabase('HelloWorld');
db.transaction(
function(transaction) {
transaction.executeSql(
'CREATE TABLE IF NOT EXISTS Table1 ' +
' (TableID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' +
' Field1 TEXT NOT NULL );'
);
}
);
db.transaction(
function(transaction) {
transaction.executeSql(
'SELECT * FROM Table1;',function (transaction, result) {
for (var i=0; i < result.rows.length; i++) {
alert('1');
$('body').append(result.rows.item(i));
}
},
errorHandler
);
}
);
$('form').submit(function() {
var xxx = $('#xxx').val();
db.transaction(
function(transaction) {
transaction.executeSql(
'INSERT INTO Table1 (Field1) VALUES (?);', [xxx], function(){
alert('Saved!');
},
errorHandler
);
}
);
return false;
});
});
function errorHandler(transaction, error) {
alert('Oops. Error was '+error.message+' (Code '+error.code+')');
transaction.executeSql('INSERT INTO errors (code, message) VALUES (?, ?);',
[error.code, error.message]);
return false;
}
</script>
</head>
<body>
<form method="post">
<input name="xxx" id="xxx" />
<p>
<input type="submit" name="OK" />
</p>
<a href="http://www.google.com">Cancel</a>
</form>
</body>
</html>
I'm trying to learn about openDatabase, and I think I'm getting it to INSERT INTO TABLE1, but I can't verify that the SELECT * FROM TABLE1 is working.
<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript">
var db;
$(function(){
db = openDatabase('HelloWorld');
db.transaction(
function(transaction) {
transaction.executeSql(
'CREATE TABLE IF NOT EXISTS Table1 ' +
' (TableID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' +
' Field1 TEXT NOT NULL );'
);
}
);
db.transaction(
function(transaction) {
transaction.executeSql(
'SELECT * FROM Table1;',function (transaction, result) {
for (var i=0; i < result.rows.length; i++) {
alert('1');
$('body').append(result.rows.item(i));
}
},
errorHandler
);
}
);
$('form').submit(function() {
var xxx = $('#xxx').val();
db.transaction(
function(transaction) {
transaction.executeSql(
'INSERT INTO Table1 (Field1) VALUES (?);', [xxx], function(){
alert('Saved!');
},
errorHandler
);
}
);
return false;
});
});
function errorHandler(transaction, error) {
alert('Oops. Error was '+error.message+' (Code '+error.code+')');
transaction.executeSql('INSERT INTO errors (code, message) VALUES (?, ?);',
[error.code, error.message]);
return false;
}
</script>
</head>
<body>
<form method="post">
<input name="xxx" id="xxx" />
<p>
<input type="submit" name="OK" />
</p>
<a href="http://www.google.com">Cancel</a>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你就快到了,只有两件事:
executeSql
方法使用参数数组参数,即使您的查询不需要任何参数。result.rows.item(i)
是一个包含属性作为表字段的对象。您的选择将像这样工作:
此处检查您的示例。
You're almost there, there are only two things:
executeSql
method the parameters array argument, even if your query doesn't need any param.result.rows.item(i)
is an object that contains properties as your table fields.Your select will work like this:
Check your example here.