我需要做多个插入物,我不知道怎么了。请参阅:
db.openDatabase({
name: "data.db",
location: "default"
}).then(() => {
db.executeSql(
+"INSERT INTO check_item (name) VALUES ('Pneus - calibragem'); "
+"INSERT INTO check_item (name) VALUES ('Pneus – banda de rodagem'); "
+"INSERT INTO check_item (name) VALUES ('Nivel do oleo'); "
+"INSERT INTO check_item (name) VALUES ('Luzes dianteiras'); "
+"INSERT INTO check_item (name) VALUES ('Luzes traseiras'); "
+"INSERT INTO check_item (name) VALUES ('Triangulo de sinalizacao'); "
+"INSERT INTO check_item (name) VALUES ('Chave de roda'); "
+"INSERT INTO check_item (name) VALUES ('Documentacao do veiculo'); ", {}).then((data) => {
console.log("TABLE CREATED: ", data);
}, (error) => {
console.error("Unable to execute sql teste", error);
})
}, (error) => {
console.error("Unable to open database", error);
});
我该如何修复?
您当前正在尝试在单个呼叫executeSql()
中执行多个插入语句。用于在单个语句中插入多个记录的语法是:
INSERT INTO check_item (name)
VALUES ('Pneus - calibragem'),
('Pneus – banda de rodagem'),
('Nivel do oleo'),
...
('Documentacao do veiculo');
如果要防止插入已经存在的名称的可能性,那么最简单的方法可能是在name
列上添加唯一的约束。虽然不可能在不重新创建桌子的情况下在sqlite中执行此操作,但我们可以添加一个唯一的索引来获得相同的效果:
CREATE UNIQUE INDEX name_index ON check_item (name);
进一步阅读:如何在SQLITE iOS中已经制造的表中添加唯一约束?
插入tbl_name (A,B,C)值 (1,2,3), (4,5,6), (7,8,9);