Skip to content

How to use SQL file as query source #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EdoardoPedrotti opened this issue May 16, 2014 · 3 comments
Closed

How to use SQL file as query source #819

EdoardoPedrotti opened this issue May 16, 2014 · 3 comments

Comments

@EdoardoPedrotti
Copy link

I'm trying to execute a .sql script with the source command. This is my code:

function sourceDump(callback) {
        console.log('   Using totem database.');
        dbConn.query('use totem;', function(err, rows, fields) {
            if (err) {
                console.log(err);
                console.log('   Unable to change database.')
                callback();
                return;
            }
            console.log('   Loading dump file...');
            var path = '/home/lounge3/TotemUpdateServer/data/dump/totem.sql';
            dbConn.query('source /home/lounge3/TotemUpdateServer/data/dump/totem.sql;', function (err, rows, fields) {
                if (err) {
                    console.log(err);
                    console.log('   Unable to execute script.');
                    callback();
                    return;
                }
                console.log('   Dump executed.');
                callback();
            });
        });

The same command works fine in the mysql command line. When i try to use it with node.js and the mysql module I get this error:

{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source /home/lounge3/TotemUpdateServer/data/dump/totem.sql' at line 1]
  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlState: '42000',
  index: 0 }
@dougwilson
Copy link
Member

"source" is not a MySQL command, but rather a comment that is specific to the mysql command like program (http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html)

To execute a SQL script using this library, you will need to read the file into node as a string and then send that string as a query. You will want to also enable multi statements. I'll have a follow up comment with an example.

@dougwilson
Copy link
Member

Simple example:

var fs = require('fs');
var mysql = require('mysql');

var connection = mysql.createConnection({
  multipleStatements: true, // because your file probably contains multiple statements
  // your settings here
});

var source = fs.readFileSync('/home/lounge3/TotemUpdateServer/data/dump/totem.sql', 'utf8');

connection.query(source, function(err){
  if (err) throw err;
  console.log('done!');
  connection.end();
});

@EdoardoPedrotti
Copy link
Author

Thank you! That really helped!

@dougwilson dougwilson added the FAQ label May 16, 2014
@dougwilson dougwilson changed the title ER_PARSE_ERROR while using source command How to use SQL file as query source May 16, 2014
dveeden pushed a commit to dveeden/mysql that referenced this issue Jan 31, 2023
json encoded data is represented as bytes however it should be interpolated as a string

Fixes mysqljs#819
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants