csol.db
v1.2.3
Published
Provide MSSQL PostgreSQL SQLite MySQL unified connection class
Downloads
6
Readme
##简介 提供 mssql postgresql sqlite mysql统一连接类 提供方法 init, query, exec, execSync, sleep, insert, update, del, escape, close
##示例代码
var db = require("csol.db");//引入数据库
var argvs = require('minimist')(process.argv.slice(2));
var config;
var starttime=new Date();
switch(argvs.d){
case "mysql":
config = {
dbtype: "mysql",
host: "localhost",
user: "kensite_cms",
password: "DMHWceLdSpZ6FYWS",
database: "kensite_cms",
}
break;
case "mssql":
config = {
dbtype:"mssql",
server: "127.0.0.1",
user: "sa",
password: "dev@123,",
database: "master",
port:14330,
options: {
enableArithAbort:false,
encrypt: false // Use this if you're on Windows Azure
},
pool: {
min: 0,
max: 10,
idleTimeoutMillis: 3000
}
}
break;
case "postgresql":
config = {
dbtype:"postgresql",
user: "postgres",
database: "",
password: "",
port: 5432,
// 扩展属性
max: 20, // 连接池最大连接数
idleTimeoutMillis: 3000, // 连接最大空闲时间 3s
}
break;
case "sqlite":
config = {
dbtype: "sqlite",
file: process.cwd()+"/mydb.db"
}
break;
default:
console.log(" args is -d base mssql postgresql sqlite mysql")
process.exit();
break;
}
db.init(config,function(id,error){
console.log(id,error);
});
db.exec(DROP TABLE IF EXISTS test;
,function(err){
console.log(err);
});
db.exec(create table test(
id int,
title char(255) ,
body char(255)
)
,function(data,fields,error){
console.log(data,fields,error)
})
db.del("test")
for(var i=1;i<=60;i++) { db.insert("test",{"id":i,"title":"test"+i,"body":"test0 and ; -- alert('ok');"+String.fromCharCode(i)}); }
db.update("test",{"body":"hellow a bc"},{"id":1},function(data,fields,error){ if(error){ console.log(error); }else{ console.log(data); } })
db.query("select * from test where id<=50 order by id asc",function(data,fields,error){ if(error){ // console.log(error); }else{ for(var i = 0; i < data.length; i++){ var item=data[i]; console.log(item.id,item.title,item.body) }
}
console.log("execute time:",new Date()-starttime)
}); db.close();