SQLite INSERT 和 DELETE 查询给出错误,但不是 SELECT 查询
Posted
技术标签:
【中文标题】SQLite INSERT 和 DELETE 查询给出错误,但不是 SELECT 查询【英文标题】:SQLite INSERT and DELETE query is giving error, but not SELECt query 【发布时间】: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);
);
);
我搜索了几十个讨论表单,但找不到一个解决此问题的帖子。 我们将不胜感激并提前感谢您的帮助。
【问题讨论】:
【参考方案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);
);
);
【讨论】:
以上是关于SQLite INSERT 和 DELETE 查询给出错误,但不是 SELECT 查询的主要内容,如果未能解决你的问题,请参考以下文章