本文整理汇总了TypeScript中sql.js.Database类的典型用法代码示例。如果您正苦于以下问题:TypeScript js.Database类的具体用法?TypeScript js.Database怎么用?TypeScript js.Database使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了js.Database类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: require
import fs = require("fs");
import * as SQL from "sql.js";
var DB_PATH = "data.db";
function createFile(path: string): void {
var fd = fs.openSync(path, "a");
fs.closeSync(fd);
}
// Open the database file. If it does not exist, create a blank database in memory.
var databaseData: Buffer;
databaseData = fs.existsSync(DB_PATH) ? fs.readFileSync(DB_PATH) : null;
var db = new SQL.Database(databaseData);
// Create a new table 'test_table' in the database in memory.
var createTableStatement =
"DROP TABLE IF EXISTS test_table;" +
"CREATE TABLE test_table (id INTEGER PRIMARY KEY, content TEXT);";
db.run(createTableStatement);
// Insert 2 records for testing.
var insertRecordStatement =
"INSERT INTO test_table (id, content) VALUES (@id, @content);";
db.run(insertRecordStatement, {
"@id": 1,
"@content": "Content 1"
});
db.run(insertRecordStatement, {
"@id": 2,
"@content": "Content 2"
示例2: dbAccessDone
function dbAccessDone(): void {
// Save the database into SQLite version 3 format.
if (!fs.existsSync(DB_PATH)) {
createFile(DB_PATH);
}
var exportedData = db.export();
fs.writeFileSync(DB_PATH, exportedData);
// Finally, close the database connection and release the resources in memory.
db.close();
}