nodejs
如果你了解过nodejs的话,剩下的就简单了。安装node-sqlite3
这里用到的模块就是这个了[node-sqlite3][0]
npm install sqlite3
node-sqlite3 api
常用的API大致有下面这些- - new sqlite3.Database(filename, [mode], [callback])
- - Database#run(sql, [param, ...], [callback])
- - Database#get(sql, [param, ...], [callback])
- - Database#all(sql, [param, ...], [callback])
- - Database#each(sql, [param, ...], [callback], [complete])
- - Database#exec(sql, [callback])
- - Database#prepare(sql, [param, ...], [callback])
- - ...
db = new sqlite3.Database(‘chain.sqlite3‘, createTable); db.run("CREATE TABLE IF NOT EXISTS lorem (info TEXT)", insertRows); var stmt = db.prepare("INSERT INTO lorem VALUES (?)"); db.all("SELECT rowid AS id, info FROM lorem", function(err, rows) {}); db.close();与现有的数据库协作(转载保留:网站重构)
数据库的名字叫
sqlite3.db我们需要执行的查询命令是
SELECT id,content,title,description FROM blog_blogpost
最终代码
"use strict"; var sqlite3 = require(‘sqlite3‘).verbose(); var db = new sqlite3.Database(‘sqlite3.db‘); function readAllRows() { console.log("readAllRows lorem"); db.all("SELECT id,content,title,description FROM blog_blogpost", function(err, rows) { rows.forEach(function (row) { console.log(row.id + ": " + row.title); }); closeDb(); }); } function closeDb() { console.log("closeDb"); db.close(); } readAllRows();运行一下
node sqlite3.js结果如下所示
readAllRows lorem 1: ... 215: rhino javascript,用rhino搭建本地的javascript环境 216: be a geek 8:无处不在的三剑客 217: dip 依赖倒置原则小试 218: OO notes 面向对象编程笔记 closeDb
最后效果可见:Phodal‘s New Homepage