Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute mysql dump file to create table and feed data in nodejs project

Tags:

node.js

mysql

I am working on nodejs project. I have following mysql dump file. Rather go on and creating that table manually and feed data manually using mysql queries, I want to execute following dump file which will create the table and feed/insert the data in that table. How can I do that with command?

  CREATE TABLE employees (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(50),
    location varchar(50),
    PRIMARY KEY (id)
  ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

  INSERT INTO employees (id, name, location) VALUES
  (1, 'Jasmine', 'Australia'),
  (2, 'Jay', 'India'),
  (3, 'Jim', 'Germany'),
  (4, 'Lesley', 'Scotland');

UPDATE

I want something similar in node js like below is in Ruby on rails

Populating database using seeds.rb

Or in Laravel is here

like image 625
Om3ga Avatar asked Jun 25 '26 15:06

Om3ga


1 Answers

The way to run an SQL script is by opening a process to execute the mysql command-line client with the SQL script as input.

Some people try to split the SQL file, and run statements one at a time. But this doesn't work in general. There are some statements in a dump file (e.g. DELIMITER) that cannot be run against the server, because they're builtin commands for the mysql client.

Here's a past Stack Overflow question that shows how to use child_process to spawn the mysql client:

  • Spawn a mysql process to import a database using node
like image 108
Bill Karwin Avatar answered Jun 28 '26 06:06

Bill Karwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!