简体   繁体   中英

Create DB from a dump.sql file with NodeJS (Express) + TypeScript

I'm using Express and TypeScript to build an app. This app is already configured to work with MySQL but my problem is that I don't know yet how to create the database based on a dump.sql file.

CREATE DATABASE IF NOT EXISTS test;

USE test;

DROP TABLE IF EXISTS testData;

CREATE TABLE testData(
  name VARCHAR(20) NOT NULL,
  valid INT NULL DEFAULT NULL,
  PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO testDataVALUES
('Peter', NULL),
.
.
.

This is what I'm trying to find, passing the sql file as variable or something after being exported or something from it file because with the.sql extension I can't export the file.

connection.query(dump.sql, function (err, rows, fields) {
   if (err) throw err;
   console.log('The database was successfully created..');
});

So is it possible to do it? I would like to know HOW because I don't see any documentation up to date related to this... otherwise I will create the DB manually in xampp and after use the db but I think this is not the way to do it.

Thanks!!!

If this is a dump file produced by mysqldump then you should just execute a shell command to run the mysql client and source that dump file as its input.

The reason is that dump files may contain mysql client builtin commands that are not recognized by the MySQL Server. They are preprocessed by the mysql client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM