2016-02-15 31 views
0

插入元素我想插入一些元素在我的SQLite數據庫,但是它的輸出顯示什麼:與sqlite3的

var fs = require("fs"); 
var file = "Res.db"; 
var exists = fs.existsSync(file); 

if(!exists) { 
    console.log("Creating DB file."); 
    fs.openSync(file, "w"); 
} 

var sqlite3 = require("sqlite3").verbose(); 
var db = new sqlite3.Database(file); 

function insertScore(sc, id){ 
console.log("Insert."); 
db.run("INSERT INTO Score (score, uid) VALUES (?,?)", sc, id); 
    console.log("End Insert."); 
} 

function createTable() { 
console.log("Creating Table."); 
    if(!exists) { 
    db.run("CREATE TABLE Score (score TEXT, uid TEXT)"); 
    } 
} 

function closeDb() { 
    db.close(); 
} 

createTable(); 
insertScore("18.59", "toto"); 
db.each("SELECT uid, score FROM Score", function(err, row){ 
console.log(row.score + " = " + row.uid); 
}); 
closeDb(); 

我不知道要插入數據的正確方法。我是全新的網絡資訊。我只是使用sqlite,因爲它是一個小型的私人項目,並且練習一點點的web環境。

感謝您的幫助

+0

這段代碼對我來說工作的很好,但是第一次在創建表之前運行'INSERT'。一旦表存在的代碼應該工作正常,雖然 –

+0

感謝您的答案,但我從來沒有創建我的表之前調用INSERT,是嗎? – king

+0

好吧,如果有人有線索,我有一個沒有這樣的表分數錯誤。我不明白爲什麼。 – king

回答

0

那麼它出現的運行是一個異步調用所以...嗯,這是夢幻般|°_°|