javascript Websql 插入查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19946012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Websql Insert Query
提问by Gopesh
I am new websql.i cannot insert primary key in websql.
我是新的 websql。我无法在 websql 中插入主键。
Create Query
创建查询
db.transaction(function (tx) {
var sql =
"CREATE TABLE vehicles ( "+
"id integer primary key autoincrement,"+
"vehicle_ref_no VARCHAR(255),"+
"vehicle_plate_no VARCHAR(255),"+
)";
tx.executeSql(sql);
});
Insert Query
插入查询
db.transaction(
function (tx) {
tx.executeSql(
"INSERT INTO vehicles VALUES (?, ?)",
[data.vehicle_ref_no,
data.vehicle_plate_no,
],
function (tx, result) {
console.log("Query Success");
},
function (tx, error) {
console.log("Query Error: " + error.message);
}
);
},
function (error) {
console.log("Transaction Error: " + error.message);
},
function () {
console.log("Transaction Success");
}
);
This code doesnot add the autoincrement value ,Any ideas? Is there any good method for doing this other than what i do?
这段代码没有添加自动增量值,有什么想法吗?除了我所做的之外,还有什么好的方法可以做到这一点吗?
回答by peterm
Your INSERT
statement is incorrect. You should either include names of columns other then id
你的INSERT
说法是不正确的。您应该包括其他列的名称id
INSERT INTO vehicles (vehicle_ref_no, vehicle_plate_no) VALUES (?, ?);
or pass NULL for id
或将 NULL 传递给 id
INSERT INTO vehicles (id, vehicle_ref_no, vehicle_plate_no) VALUES (NULL, ?, ?);
or just
要不就
INSERT INTO vehicles VALUES (NULL, ?, ?);
Here is SQLFiddledemo
这是SQLFiddle演示
回答by Pradip Kushare
public insertData(vehicle_details) {
let insert_query = "INSERT INTO vehicles VALUES (?, ?)";
this.db.executeSql(insert_query, [vehicle_details.vehicle_ref_no, vehicle_details.vehicle_plate_no]).then((data) => {
console.log(' records inserted');
}), (error) => {
console.log('ERROR occurred in inserting records');
}
}