You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to execute a .sql script with the source command. This is my code:
functionsourceDump(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...');varpath='/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 }
The text was updated successfully, but these errors were encountered:
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.
varfs=require('fs');varmysql=require('mysql');varconnection=mysql.createConnection({multipleStatements: true,// because your file probably contains multiple statements// your settings here});varsource=fs.readFileSync('/home/lounge3/TotemUpdateServer/data/dump/totem.sql','utf8');connection.query(source,function(err){if(err)throwerr;console.log('done!');connection.end();});
I'm trying to execute a .sql script with the source command. This is my code:
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:
The text was updated successfully, but these errors were encountered: