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