本文整理汇总了TypeScript中sqlite3.Database.prepare方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Database.prepare方法的具体用法?TypeScript Database.prepare怎么用?TypeScript Database.prepare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlite3.Database
的用法示例。
在下文中一共展示了Database.prepare方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: lorem
db.serialize(() => {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
console.log(row.id + ": " + row.info);
});
});
示例2: it
it('works',async ()=>{
var db = new Database(dbPath);
let sqltext = "insert or replace into things_store (key,value) values ('z', 'z')";
console.time('sqlite insert');
await new Promise((rs, rj)=>{
db.exec(sqltext, err=>{
if(err){
rj(err);
return;
}
rs(true);
});
});
console.timeEnd('sqlite insert');
console.time('sqlite insert 2');
await new Promise((rs, rj)=>{
db.exec(sqltext, err=>{
if(err){
rj(err);
return;
}
rs(true);
});
});
console.timeEnd('sqlite insert 2');
//Statement
let insertCmd = "insert or replace into things_store (key,value) values ($key, $value)";
var params = { $key: 'z', $value: 'z'};
var statement = db.prepare(insertCmd);
console.time('sqlite insert statement');
await new Promise((rs, rj)=>{
statement.run( params ,err=>{
if(err){
rj(err);
return;
}
rs(true);
});
});
console.timeEnd('sqlite insert statement');
console.time('sqlite insert statement 2');
await new Promise((rs, rj)=>{
statement.run( params ,err=>{
if(err){
rj(err);
return;
}
rs(true);
});
});
console.timeEnd('sqlite insert statement 2');
params.$key = 'q1';
console.time('sqlite insert statement 3');
await new Promise((rs, rj)=>{
statement.run( params ,err=>{
if(err){
rj(err);
return;
}
rs(true);
});
});
console.timeEnd('sqlite insert statement 3');
console.time('sqlite insert statement 4');
statement.run( params ,err=>{
if(err){
throw err;
}
});
console.timeEnd('sqlite insert statement 4');
let key = 'z';
console.time('sqlite get');
var data = await new Promise((rs,rj)=>{
db.all(`select * from things_store where key = '${key}'`, (err, data)=>{
if(err){
rj(err);
return;
}
rs(data);
});
});
console.timeEnd('sqlite get');
assert.equal(data[0].key, 'z');
assert.equal(data[0].value, 'z');
})