我正在学习mo的节点js,并使用SQLite3创建一个可移植的项目/任务/工作流工具(它最终将最终在github上)。
如果这是用户第一次运行节点应用程序(并且SQLite数据库文件不存在),我希望它运行安装SQL脚本来创建它需要工作的所有表。
我可以将它们作为单独的交易来完成,但这看起来很混乱,并且在一次通话中完成所有操作要容易得多。
在Oracle中,我知道我可以将它们全部包装成如下:
BEGIN CREATE TABLE1...; CREATE TABLE2...; END;
但SQLite,我不太了解(并且不知道我是否可以做我想做的事情)。
以下代码仅创建第一个表(而不是其他表)。
感谢。
Express Node 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 );
答案 0 :(得分:3)
看起来无法完成...在Stack Overflow上找到了类似问题的答案: