【问题标题】:SQLite INSERT and DELETE query is giving error, but not SELECt querySQLite INSERT 和 DELETE 查询给出错误,但不是 SELECT 查询
【发布时间】:2021-07-13 07:17:15
【问题描述】:

我正在使用 SQLite(WebSQL) 在我的 Cordova 移动应用程序中本地保存表单数据。
下面的代码适用于同一表的 SELECT 查询,但不适用于 INSERT 或 DELETE 查询。

输出错误为:could not prepare statement

const usersDB = {
    add: function (firstname, lastname, dob, email, phone, city, state, country) {
        databaseHandler.db.readTransaction(
            function (tx) {
                tx.executeSql(
                    "INSERT INTO users(firstname, lastname, dob, email, phone, city, state, country) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
                    [firstname, lastname, dob, email, phone, city, state, country],
                    function (tx, results) {
                        console.log('User has been saved');
                    },
                    function (tx, error) {
                        console.log('ERROR: Failed to add user', error);
                    }
                );
            }
        );
    },
    selectAll: function (listUsers) {
        databaseHandler.db.readTransaction(
            function (tx) {
                tx.executeSql(
                    "SELECT * FROM users ORDER BY firstname",
                    [],
                    function (tx, results) {
                        listUsers(results);
                    },
                    function (tx, error) {
                        console.log('ERROR: Failed to list users', error);
                    }
                );
            }
        );
    }
}

我搜索了几十个讨论表单,但找不到一个解决此问题的帖子。
我们将不胜感激并提前感谢您的帮助。

【问题讨论】:

    标签: javascript sqlite cordova web-sql


    【解决方案1】:

    读取事务仅用于读取。写事务允许读取和写入。读取事务由 SELECT 语句启动,写入事务由 CREATE、DELETE、DROP、INSERT 或 UPDATE 等语句(统称为“写入语句”)启动。

    .readTransaction() 用于只读查询,.transaction() 用于写查询。
    所以就我而言,我应该以这种方式使用它们:

    const usersDB = {
        add: function (firstname, lastname, dob, email, phone, city, state, country) {
            databaseHandler.db.transaction( // Write mode
                function (tx) {
                    tx.executeSql(
                        "INSERT INTO users(firstname, lastname, dob, email, phone, city, state, country) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
                        [firstname, lastname, dob, email, phone, city, state, country],
                        function (tx, results) {
                            console.log('User has been saved');
                        },
                        function (tx, error) {
                            console.log('ERROR: Failed to add user', error);
                        }
                    );
                }
            );
        },
        selectAll: function (listUsers) {
            databaseHandler.db.readTransaction( // read-only mode
                function (tx) {
                    tx.executeSql(
                        "SELECT * FROM users ORDER BY firstname",
                        [],
                        function (tx, results) {
                            listUsers(results);
                        },
                        function (tx, error) {
                            console.log('ERROR: Failed to list users', error);
                        }
                    );
                }
            );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-03
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      相关资源
      最近更新 更多