在Node.js中从sqlite3 .sql导出创建数据库表

时间:2018-10-04 16:42:21

标签: node.js sqlite

我正在使用SQLiteStudio导出sqlite数据库,该数据库为我提供了一个如下所示的脚本;

//export.sql
--
-- File generated with SQLiteStudio v3.1.1 on Thu Oct 4 17:25:52 2018
--
-- Text encoding used: UTF-8
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Table: Accounts
DROP TABLE IF EXISTS Accounts;

CREATE TABLE Accounts (
    Account_ID   VARCHAR PRIMARY KEY,
    Account_Name VARCHAR
);

INSERT INTO Accounts (Account_ID, Account_Name) VALUES ('ABC1234', 'ABC Company Ltd');
INSERT INTO Accounts (Account_ID, Account_Name) VALUES ('XYZ3333', NULL);

-- Table: AccountsInfo
DROP TABLE IF EXISTS AccountsInfo;

CREATE TABLE AccountsInfo (
    ID                 INTEGER       PRIMARY KEY,
    Account_ID         VARCHAR       REFERENCES Accounts (Account_ID),
    Number_Of_Payments INTEGER,
);


COMMIT TRANSACTION;
PRAGMA foreign_keys = on;

然后在我的节点脚本中执行以下操作;

const sqliteDB = new sqlite3.Database(dbPath);
sqliteDB.serialize(function() {
    const installSqlFile = path.resolve(__dirname, '../../db/export.sql');
    const installSql = fs.readFileSync(installSqlFile, 'utf-8');
    sqliteDB.run(installSql);
});

但是没有创建数据库表。在nodejs中执行此sql脚本的正确方法是什么,以便创建数据库表并插入值。

0 个答案:

没有答案