Node.js操作SQLite3

来源(博客频道 - CSDN.NET)

From: http://blog.csdn.net/mdl206/article/details/50439463

Node.js操作SQLite3

标签: node.js sqlite3

2015-12-31 08:50 427人阅读

分类:node.js

Node.js操作SQLite3

转自:Node.js操作SQLite3

npm install sqlite3 --save

var fs = require("fs");
var file = process.env.CLOUD_DIR + "/" + "test.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);

db.serialize(function() {
		if(!exists) {
			db.run("CREATE TABLE Stuff (thing TEXT)");
		}

		var stmt = db.prepare("INSERT INTO Stuff VALUES (?)");

		//Insert random data
		var rnd;
		for (var i = 0; i < 10; i++) {
			rnd = Math.floor(Math.random() * 10000000);
			stmt.run("Thing #" + rnd);
		}

		stmt.finalize();
		db.each("SELECT rowid AS id, thing FROM Stuff", function(err, row) {
				console.log(row.id + ": " + row.thing);
		});
});

db.close();

Link: http://www.asm32.net/article_details.aspx?id=7103


浏览次数 0 发布时间 2016-10-27 10:35:20 从属分类 node.js 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1