0
我正在學習mo中的節點js,並使用SQLite3創建一個可移植的項目/任務/工作流工具(它最終會在github上結束)。SQLite3 - 在一個語句中創建多個表
如果這是用戶第一次運行節點應用程序(並且SQLite數據庫文件不存在),我希望它運行一個安裝SQL腳本來創建需要工作的所有表。
我可以將它們全部作爲單獨的事務處理,但看起來很雜亂,而且在一次調用中完成這一切變得更加容易。
在Oracle我知道我可以換他們都在如下:
BEGIN CREATE TABLE1...; CREATE TABLE2...; END;
SQLite的,雖然,我不知道一樣關於(不知道如果我可以做什麼,我想做)。
以下代碼僅創建第一個表格(而不是其他)。
謝謝。
快速結點JS調用的應用程序在啓動時,SQL
// Declare Express
var express = require('express');
// Instantiate Express
var app = express();
// Create a http server with express
var server = require('http').createServer(app);
// Instantiate socket on the http express server
var io = require('socket.io').listen(server);
// Declare filesystem
var fs = require('fs');
// Database File
var db_file = './sqlite/db-rhubarb.sqlite';
// Check if the dataexists
var db_exists = fs.existsSync(db_file);
// Declare sqlite3
var sqlite3 = require('sqlite3').verbose();
// Instantiate sqlite3 database
var db = new sqlite3.Database(db_file);
// Check if a database exists - create if on first run
db.serialize(function() {
//if (!fs.existsSync(db_file)) {
console.log("Can't find a SQLite database, creating one now...");
var install_sql = fs.readFileSync('./sqlite/sql/install.sql', 'utf-8');
db.run(install_sql);
//}
});
// Application port (this is what eg localhost:1227)
var port = 1127;
// Tell server listen on port
server.listen(port);
console.log("Running application on port: "+port);
安裝SQL
/* Create the contacts table */
CREATE TABLE IF NOT EXISTS 'main'.'contacts' ("contact_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"date_added" DATETIME DEFAULT CURRENT_TIMESTAMP,
"user_id_created" INTEGER);
/* Create the contacts attribute table */
CREATE TABLE IF NOT EXISTS "main"."contact_attributes" ("cont_attr_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"contact_id" INTEGER,
"date_created" DATETIME DEFAULT CURRENT_TIMESTAMP,
"user_id_created" INTEGER,
"attr_name" VARCHAR,
"attr_value" VARCHAR);
/* Create the task table */
CREATE TABLE IF NOT EXISTS "main"."tasks" ("task_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"date_created" DATETIME DEFAULT CURRENT_TIMESTAMP,
"user_id_created" INTEGER,
"task_value" VARCHAR);
/* Create the tag table */
CREATE TABLE IF NOT EXISTS "main"."tags" ("tag_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"date_created" DATETIME DEFAULT CURRENT_TIMESTAMP,
"user_id_created" INTEGER,
"tag_value" VARCHAR UNIQUE);
/* Create the tag mapping table */
CREATE TABLE IF NOT EXISTS "main"."map_tag_task" ("map_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"date_created" DATETIME DEFAULT CURRENT_TIMESTAMP,
"user_id_created" INTEGER,
"tag_id" INTEGER,
"task_id" INTEGER);
/* Create the log table */
CREATE TABLE IF NOT EXISTS "main"."logs" ("log_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"date_created" DATETIME DEFAULT CURRENT_TIMESTAMP,
"user_id_created" INTEGER,
"task_id" INTEGER,
"log_value" VARCHAR);
/* Create the task messages table */
CREATE TABLE IF NOT EXISTS "main"."messages" ("message_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"date_created" DATETIME DEFAULT CURRENT_TIMESTAMP,
"user_id_created" INTEGER,
"task_id" INTEGER,
"message_value" VARCHAR);
/* Create the users table */
CREATE TABLE IF NOT EXISTS "main"."users" ("user_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
"forename" VARCHAR,
"surname" VARCHAR,
"password" VARCHAR,
"salt" VARCHAR);