2017-03-23 150 views
0

我只想創建一列並向其中添加一些值,但出現兩個錯誤。一個是如果我取消註釋db.close();手柄關閉太快,另一個是Error: SQLITE_ERROR: near "0": syntax error下面是代碼。錯誤:SQLITE_ERROR:接近「0」:語法錯誤

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

好吧我是個白癡,我根本就沒有關閉括號中,忽略以前的錯誤,並只關注數據庫關閉 – worer

回答

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(); 
}); 
相關問題