本文整理匯總了TypeScript中sqlite3.Database類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Database類的具體用法?TypeScript Database怎麽用?TypeScript Database使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Database類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Promise
return new Promise((resolve, reject) => {
let database = new sqlite3.Database("data.sqlite");
database.serialize(() => {
database.run("create table if not exists [data] ([council_reference] text primary key, [description] text, [on_notice_to] text, [address] text, [info_url] text, [comment_url] text, [date_scraped] text)");
resolve(database);
});
});
示例2: setupDatabase
/**
* Caution: resets database.
*/
static setupDatabase() {
try {
fs.unlinkSync(DB_PATH);
} catch (err) { }
let db = new Database(DB_PATH);
try {
db.serialize(() => {
db.run(`
CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
username TEXT NOT NULL UNIQUE,
salt TEXT NOT NULL,
hashedpwd TEXT NOT NULL,
admin INTEGER NOT NULL DEFAULT 0
);
`);
db.run(`
CREATE TABLE entries (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
date TEXT NOT NULL,
hungerscore INTEGER NOT NULL,
hungertext TEXT,
cravingscore INTEGER NOT NULL,
cravingtext TEXT,
satietyscore INTEGER NOT NULL,
satietytext TEXT,
energyscore INTEGER NOT NULL,
energytext TEXT,
wellbeingscore INTEGER NOT NULL,
wellbeingtext TEXT,
carbsscore INTEGER NOT NULL,
carbstext TEXT,
stressambool INTEGER NOT NULL,
stresspmbool INTEGER NOT NULL,
walksbool INTEGER NOT NULL,
movementbool INTEGER NOT NULL,
movementtext TEXT,
bedtimebool INTEGER NOT NULL,
bedtimetext TEXT,
userid INTEGER NOT NULL,
FOREIGN KEY(userid) REFERENCES user(id)
);
`);
});
}
catch (err) { console.error(err); }
finally {
db.close();
}
}
示例3: Promise
return new Promise((resolve, reject) => {
let db = new Database(DB_PATH);
return db.all(
"SELECT * FROM entries WHERE userid = ? ORDER BY date",
[userId],
(err, rows) => {
if (!rows) {
reject(err);
} else {
if (err) { console.error(err); }
resolve(rows);
}
}
);
});
示例4: users
.then(data => {
db.run(
"insert into users (username, salt, hashedpwd) values (?, ?, ?)",
[data.username, data.salt, data.hash], cb // Null on success, single error parameter on success
);
db.close();
});
示例5: VALUES
.then((data) => {
console.log(data.cols);
console.log(data.records);
db.run("INSERT INTO memos VALUES('TEST', 5000)");
db.each("SELECT * FROM memos", (err, row) => {
console.log(row.text);
});
})
示例6: 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);
});
});
示例7: users
database.all("SELECT * FROM users WHERE user_id=$userid", { $userid: userid }, (err, rows) => {
if (rows.length < 1) {
database.run("INSERT INTO users(user_id, lastfm_name) VALUES ($userid, $lastfm)", { $userid: userid, $lastfm: lastfmName }, (err) => {
if (err) reject(Util.errorMsg(err));
resolve(rows);
});
} else {
database.run("UPDATE users SET lastfm_name=$lastfm WHERE user_id=$userid", { $lastfm: lastfmName, $userid: userid }, (err) => {
if (err) reject(Util.errorMsg(err));
resolve(rows);
});
}
});
示例8: rooms
database.all("SELECT * FROM rooms WHERE room_id=$roomid", { $roomid: roomid }, (err, rows) => {
if (rows.length < 1) {
database.run("INSERT INTO rooms(room_id, auto_join) VALUES ($roomid, $status)", { $roomid: roomid, $status: status }, (err) => {
if (err) reject(Util.errorMsg(err));
resolve(rows);
});
} else {
database.run("UPDATE rooms SET auto_join=$status WHERE room_id=$roomid", { $status: status, $roomid: roomid }, (err) => {
if (err) reject(Util.errorMsg(err));
resolve(rows);
});
}
});
示例9:
ipcMain.on("getChatLogs", (event: IPCMainEvent, arg: string) => {
let param: IGetChatLogsParam = JSON.parse(arg);
db.serialize(() => {
db.each(
"SELECT channel_id, timestamp, type, nickname, user_id, message FROM chat WHERE channel_id=? AND timestamp>=? ORDER BY id",
param.channelId,
Date.now() - 1000 * 60 * 60 * 24 * 7,
(err: Error, row: any) => {
if (err === null) {
let message: IMessage = {
channelId: row.channel_id,
timestamp: row.timestamp,
type: row.type,
message: row.message,
nickname: row.nickname,
userId: row.user_id,
};
event.sender.send("getChatLogs", JSON.stringify(message));
} else {
console.log(err);
}
}
);
});
});