【问题标题】:Web SQL INSERT INTO causing a crashWeb SQL INSERT INTO 导致崩溃
【发布时间】:2016-06-10 13:15:20
【问题描述】:

我已经创建了一个数据库、一个表,并用一种​​方法预先填充了初始值:

function Setup(cb)
{
    _db = openDatabase("MoneyMan", "1.0", "Money Manager", 8 * 1024 * 1024);

    // Wallets
    _db.transaction(function(tx) {

        // Create wallet table
        tx.executeSql('CREATE TABLE Wallets (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, balance FLOAT, target FLOAT, transactions INTEGER)', [],
            function(tx, res) {

                // Add initial wallets
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Main", 0, 0, NULL)');
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Bills", 0, 0, NULL)');
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Savings", 0, 0, NULL)');
                tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Business", 0, 0, NULL)');

            });


        cb();
    });
}

这很好用。

我有一个按钮可以将新钱包添加到表中,但它不起作用。甚至这段代码也中断了:

_db.transaction(function(tx) {
        tx.executeSql('INSERT INTO Wallets VALUES (NULL, "Testing", 0, 0, NULL)');
});

编辑:我也尝试过这样列出字段:

_db.transaction(function(tx) {
        tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES (?, 0, 0, NULL)', [name]);
    });

在 Chrome 上,整个页面都中断了,我收到“Aww,Snap”。在 Android 上什么都没有发生。但是,当我刷新页面时,似乎钱包已成功添加。

我已经在整个互联网上搜索了解决此问题的方法,这让我发疯了。我哪里错了?

谢谢

编辑:

当我在 executeSQL() 上设置断点时,它运行良好。只有当它试图离开事务回调时才会中断。

编辑:

请注意,这是从另一种语言编译而来的 Javascript。变量“_db”可通过第二种方法访问。如果我在尝试添加新钱包时执行 SELECT 语句,则没有问题。只有 INSERT INTO 中断。

【问题讨论】:

  • 只是一个想法,可能有用,尝试把 _db.transaction(...);在函数内部并将其分配给该按钮的 onclick()。因为你说在 crash 后添加 item 成功,所以 SQL 查询似乎没有错误

标签: javascript android cordova web-sql


【解决方案1】:

试试

tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES ("Testing", 0, 0, NULL)');

您将 NULL 作为已自动递增的主键传递

【讨论】:

  • 我也试过这个。这是原始代码: _db.transaction(function(tx) { tx.executeSql('INSERT INTO Wallets (name, balance, target, transactions) VALUES (?, 0, 0, NULL)', [name], function( tx, res) { cb(res); } ); });
  • 只需检查您传递的参数。您正在传递 5 个参数(NULL、“Testing”、0、0、NULL),而该方案只有 4 个
  • 你有任何异常信息的堆栈跟踪吗?
  • 您确定可以使用“准备好的语句”而不是可靠的 SQL 查询吗?尝试使用不带“?”的明确 SQL 查询(准备好的陈述)
  • 堆栈跟踪只是说匿名函数。我的原始代码使用准备好的语句。
【解决方案2】:

您没有插入正确数量的值。您想像在第一次插入中一样提供字段列表。 (name, balance, target, transactions) VALUES

【讨论】:

    猜你喜欢
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2023-02-09
    • 2017-01-03
    • 2012-01-23
    相关资源
    最近更新 更多