Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use NodeJS to run an SQL file in MySQL

Tags:

I am using the mysql plugin for nodejs and it is fantastic at doing everything I need so far.

However I have come across a stumbling block. I have created a MySQL provider that exports a mysql pool:

var mysql = require('mysql'); var mysqlPool  = mysql.createPool({   host     : '127.0.0.1',   user     : 'root',   password : '' });  mysqlPool.getConnection(function(err, connection) {   connection.query("INSERT INTO .... 

I can select, create, insert, etc all fine, but I've come across a task where I would like to run a small SQL string with about 10 different commands together. I've thought about doing one of the following:

  1. Execute a SQL file against a database using mysql
  2. Run a query and enable multipleStatements

I have written some code to execute mysql as a child process, but I would really love to avoid doing this:

var cp = require("child_process"); var cmdLine = "mysql --user=autobuild --password=something newdb < load.sql"; cp.exec(cmdLine, function(error,stdout,stderr) {     console.log(error,stdout,stderr); });   

The problem with option two is I would rather not enable multipleStatements for every query, just this one particular one. I have thought about creating a new connection, but just thinking of other ways this could be done.

TL;DR? Using NodeJS and MySQL how can I execute the following into a database:

CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20) );  CREATE TABLE sofa (name VARCHAR(20), owner VARCHAR(20) );  CREATE TABLE table (name VARCHAR(20), owner VARCHAR(20) ); 

Thanks so much for anyone who shares their ideas

like image 838
Mark Avatar asked Mar 09 '14 00:03

Mark


People also ask

How do I run a .SQL file in node JS?

The following are the steps I took. Step 1, install the NPM package called sqlite3 (Read the sqlite3 docs here). sqlite3 helps you to connect to your SQLite database and to run queries. Step 2, In your the JS file where you want to run the SQLs, import/require sqlite3 and fs (No, you don't need to install this one.

Can I use Nodejs with MySQL?

Once you have MySQL up and running on your computer, you can access it by using Node.js. To access a MySQL database with Node.js, you need a MySQL driver.

Can Nodejs be used with SQL?

You can connect to a SQL Database using Node. js on Windows, Linux, or macOS.


2 Answers

You can use the connection option called multipleStatements:

// Works with the pool too. var connection = mysql.createConnection({multipleStatements: true}); 

Then, you can pass the queries like these:

connection.query('CREATE 1; CREATE 2; SELECT 3;', function(err, results) {   if (err) throw err;    // `results` is an array with one element for every statement in the query:   console.log(results[0]); // [create1]   console.log(results[1]); // [create2]   console.log(results[2]); // [select3] }); 
like image 168
jmingov Avatar answered Oct 06 '22 13:10

jmingov


Here is a big .sql file friendly way to progmatically execute multiple queries against MySQL without using the multipleStatements property and a massive buffer. Please note this is not the most efficient way to upload to mysql.

var mysql = require('mysql'); var fs = require('fs'); var readline = require('readline'); var myCon = mysql.createConnection({    host: 'localhost',    port: '3306',    database: '',    user: '',    password: '' }); var rl = readline.createInterface({   input: fs.createReadStream('./myFile.sql'),   terminal: false  }); rl.on('line', function(chunk){     myCon.query(chunk.toString('ascii'), function(err, sets, fields){      if(err) console.log(err);     }); }); rl.on('close', function(){   console.log("finished");   myCon.end(); }); 
like image 24
Justin Avatar answered Oct 06 '22 13:10

Justin