本文整理匯總了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');
})