Trax
Trax

Reputation: 1558

node.js exec sql file

I have a sql file that contains:

DROP TABLE IF EXISTS aodb;
CREATE TABLE aodb (lowid INT, highid INT, lowql INT, highql INT, name VARCHAR(150), icon INT);
INSERT INTO aodb VALUES (275403, 275403, 1, 1, '"A History of Rubi-Ka" by Prof. Arthur B. Diggins', 136331);

It has about 275k inserts, is there a way I can use it with node? I need a way to create that table and insert the data from inside a node file w/o any kind of interaction from the user.

Upvotes: 0

Views: 5653

Answers (2)

Mingfei
Mingfei

Reputation: 1649

I have the same question today, here is my final solution:


const cp = require('child_process');

cp.exec('mysql -u<your_user> -p<your_password> < your_file.sql', (error, stdout, stderr) => {
    if (error) throw error;
    console.log(`stdout: ${stdout}`);
    console.log(`stderr: ${stderr}`);
});

Upvotes: 0

zlumer
zlumer

Reputation: 7004

Connect to your MySql database with any Node.js MySql client, read SQL from the file (using fs module) and execute it.

Upvotes: 2

Related Questions