我只想创建一个包含一列的表并为其添加一些值,但我得到两个错误。一个是如果我取消注释db.close();句柄关闭太快,另一个是Error: SQLITE_ERROR: near "0": syntax error
下面是代码。
var file = "DB.sqlite3";
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
if(!fs.existsSync(file)) fs.openSync(file, "w");
db.serialize(function() {
db.run(`CREATE TABLE IF NOT EXISTS players (id INTEGER)`);
for(var i = 0; i < 10; i++)
{
db.run(`SELECT EXISTS(SELECT 1 FROM players WHERE id=${i})`, function(error, row) {
if (row === undefined)
db.run(`INSERT INTO players (id) VALUES (?)`,i);
if(error) console.log(error);
});
}
});
//db.close(); //if I uncomment I get that the handle has closed
答案 0 :(得分:0)
我不确定,因为我只是一个初学者,但我认为你的dbclose需要进入最后一个括号内:
var file = "DB.sqlite3";
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
if(!fs.existsSync(file)) fs.openSync(file, "w");
db.serialize(function() {
db.run(`CREATE TABLE IF NOT EXISTS players (id INTEGER)`);
for(var i = 0; i < 10; i++)
{
db.run(`SELECT EXISTS(SELECT 1 FROM players WHERE id=${i})`, function(error, row) {
if (row === undefined)
db.run(`INSERT INTO players (id) VALUES (?)`,i);
if(error) console.log(error);
});
}
db.close();
});