SQLite INSERT 和 DELETE 查询给出错误,但不是 SELECt 查询
SQLite INSERT and DELETE query is giving error, but not SELECt query
我正在使用 SQLite(WebSQL) 在我的 Cordova 移动应用程序中本地保存表单数据。
下面的代码适用于相同 table 的 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);
}
);
}
);
}
}
我搜索了几十个讨论表,但找不到 post 解决这个问题的方法。
非常感谢您的帮助,并提前致谢。
A read transaction is used for reading only. A write transaction allows both reading and writing. A read transaction is started by a SELECT statement, and a write transaction is started by statements like CREATE, DELETE, DROP, INSERT, or UPDATE (collectively "write statements").
.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(WebSQL) 在我的 Cordova 移动应用程序中本地保存表单数据。
下面的代码适用于相同 table 的 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);
}
);
}
);
}
}
我搜索了几十个讨论表,但找不到 post 解决这个问题的方法。
非常感谢您的帮助,并提前致谢。
A read transaction is used for reading only. A write transaction allows both reading and writing. A read transaction is started by a SELECT statement, and a write transaction is started by statements like CREATE, DELETE, DROP, INSERT, or UPDATE (collectively "write statements").
.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);
}
);
}
);
}
}