Node-sqlite3多字段插入数据问题
在npm官网上看到sqlite3的使用方法,就去尝试了一下,奈何关于多字段插入的API简介很少,直接上代码:
官网的示例如下:
var sqlite3 = require('sqlite3').verbose(); var db = new sqlite3.Database(':memory:'); db.serialize(function() { db.run("CREATE TABLE lorem (info TEXT)"); var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); for (var i = 0; i < 10; i++) { stmt.run("Ipsum " + i); } stmt.finalize(); db.each("SELECT rowid AS id, info FROM lorem", function(err, row) { console.log(row.id + ": " + row.info); }); }); db.close();
多表插入的写法为
var sqlite3 = require('sqlite3').verbose(); var db = new sqlite3.Database(':memory:'); db.serialize(function() { db.run("CREATE TABLE lorem (info TEXT,info2 TEXT,info3 TEXT)"); var stmt = db.prepare("INSERT INTO lorem VALUES (?,?,?)"); for (var i = 0; i < 10; i++) { stmt.run(i,i,i);//多字段插入关键语法 } stmt.finalize(); db.each("SELECT rowid AS id, info FROM lorem", function(err, row) { console.log(row.id + ": " + row.info); }); }); db.close();