nodejs与sqlite

//打开数据库
var db = new sqlite3.Database(‘xx.db’);

// 关闭数据库
db.close();

db.run(‘xx’);  // 数据库对象的run函数可以执行任何的SQL语句,该函数一般不用来执行查询
               // create alter 之类的

增:
var stmt = db.prepare(“INSERT OR REPLACE INTO note (cdate, content) VALUES (?,?)”);
stmt.run(data.cdate, data.content);
stmt.finalize();

删:
db.prepare(“DELETE  from note where cdate =?”);  
stmt.run(data.cdate);  
stmt.finalize();

改:
var stmt = db.prepare(“UPDATE note set content=? where cdate =?”);  
stmt.run(data.content, data.cdate);  
stmt.finalize();  

查:
db.each(“SELECT rowid AS id, thing FROM Stuff”, function(err, row) {
    console.log(row.id + “: ” + row.thing);
  });
});
// or
db.all(“SELECT xxx”, function (err, res){});

 // 使用

先把库下载到node_modules  npm install sqlite3 --save

 1.引入sqlite3库
var sqlite3 = require(‘sqlite3’);
// or  var sqlite3 = require(“sqlite3”).verbose();

var db = new sqlite3.Database(file);
db.serialize(function() {
     //Do stuff…
     db.run(“CREATE TABLE Stuff (thing TEXT)”);

     var stmt = db.prepare(“INSERT INTO Stuff VALUES (?)”);
     for(var i = 0;i<xx;i++){
         stmt.run(‘xx’);
     }     
     stmt.finalize();

});
db.close();

 

nodejs 与sqlite
http://blog.modulus.io/nodejs-and-sqlite
http://book.51cto.com/art/201504/473574.htm

http://www.cnblogs.com/EricaMIN1987_IT/p/3654826.html

https://www.sqlite.org/

    原文作者:sqlite
    原文地址: https://www.cnblogs.com/isdom/p/webclips058.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞