开放数据库你好世界

发布于 2024-08-26 13:32:19 字数 2042 浏览 5 评论 0原文

我正在尝试了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-09-02 13:32:19

你就快到了,只有两件事:

  1. 如果你想使用 SQL 语句回调,你应该提供给 executeSql 方法使用参数数组参数,即使您的查询不需要任何参数。
  2. 您应该在循环中显示一个字段,result.rows.item(i) 是一个包含属性作为表字段的对象。

您的选择将像这样工作:

db.transaction(function(transaction) {
  transaction.executeSql('SELECT * FROM Table1',[], function (trx, result) {
    for (var i=0; i < result.rows.length; i++) {
      $('body').append(result.rows.item(i).Field1); // <-- getting Field1 value
    }
  }, errorHandler);
});

此处检查您的示例。

You're almost there, there are only two things:

  1. The if you want to use the SQL statement callback, you should supply to the executeSql method the parameters array argument, even if your query doesn't need any param.
  2. You should get a field to show in your loop, result.rows.item(i) is an object that contains properties as your table fields.

Your select will work like this:

db.transaction(function(transaction) {
  transaction.executeSql('SELECT * FROM Table1',[], function (trx, result) {
    for (var i=0; i < result.rows.length; i++) {
      $('body').append(result.rows.item(i).Field1); // <-- getting Field1 value
    }
  }, errorHandler);
});

Check your example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文