实例1:
//数据库的名字是"mydatebase.db"
var sqlite3 = require('sqlite3').verbose();
var database;
var database =
new sqlite3.Database('mydatebase.db');
//database = new sqlite3.Database("mydatebase.db", function(e){
// if (err) throw err;
//});
//也可以使用内存型,数据不会永久保存
//database = new sqlite3.Database(":memory:", function(e){
// if (err) throw err;
//});
console.log("111111111122222");
/*
database.run("CREATE TABLE foo7 (id INT,name CHAR(50))", function(e){
if(e !== null){
throw e;
}
//循环生成sql语句,批次插入多条数据
var sql = "";
var temp = "";
for(var i = 0 ; i < 500; i ++){
temp = "name"+i;
//sql += 'INSERT INTO foo6(id,name) VALUES(' + i + ' ,' +temp + ');'
sql += 'INSERT INTO foo7(id,name) VALUES(' + i + ' ,"boy345");'
}
database.exec(sql);
});
*//*
database.run("drop TABLE foo4 ", function(e){
if(e !== null){
throw e;
}
});
*/ console.log("222222222222");
//Database#each(' select * from foo ', [param, ], [callback], [complete]);
//database.all("select * from foo where id=?", idvalue, function (err, row) {});
database.all("select * from foo7 where id < 10 ",
function (err, rows) {
console.log(rows);
});
database.each(" select * from foo2 where id < 50 ",
function(err, row) {
//console.log(row.id + ": " + row.name);
});
console.log("333333333333333322222");
实例2:
//数据库的名是"mydatebase.db"
//var sqlite3 = require("sqlcipher");
var sqlite3 = require("sqlite3");
//创建数据库
var database =
new sqlite3.Database("mydatebase.db",
function(err){
if (err){
console.log("new database error,",err.message);
}
else{
console.log("new database success");
//创建表
database.run("create table if not exists sharewaf_data(time NUMERIC, domain TEXT, ip TEXT, lon_lat TEXT, address TEXT, url TEXT, type TEXT, agent TEXT)",
function(err){
if(err){
console.log("create database error,",err.message);
}
else{
console.log("create database success");
//插入数据
database.run("insert into sharewaf_data(time, domain, ip, lon_lat, address, url, type, agent) VALUES(?,?,?,?,?,?,?,?)",["time22", "domain22", "ip22", "lon_lat22", "taiyuan22", "url22", "type", "agent"],
function(err){
if(err){
console.log("insert data error,",err.message);
}
else{
console.log("insert data success");
//查询
database.all("select * from sharewaf_data",
function(err, rows) {
if(err){
console.log("select from sharewaf_data error,",err.message);
}
else{
console.log(rows);
}
});
}
});
}
});
}
});