Im trying to use node.js for the creation of a mysql database and tables.
The query i want to execute is:
CREATE DATABASE NodeTest;USE NodeTest;CREATE TABLE tblUsers (userId varchar(32), userName varchar(255), friendlyUserName varchar(255));
So i execute more queries all at once. In MySqlWorkbench this is working just fine.
In my nodejs project it says there is a syntax error in my sql statement.
my node code:
mysqlConnection.query( 'CREATE DATABASE NodeTest;USE NodeTest;CREATE TABLE tblUsers (userId varchar(32), userName varchar(255), friendlyUserName varchar(255));' , function( oError ){
if ( oError ) throw oError;
console.log( 'Database created.' );
});
Why cant i use my query in this way in node js but the syntax is working in MySql workbench just fine? Am i missing something?
SIDENOTE:
im using node-mysql (https://github.com/felixge/node-mysql) for my mysql operations.
According to there documentation, it is possible co concatenate queries, but the example in there documentation only talks about select queries.
I hate to anwser my own question, but i found the solution.
I miss red the documentation. Its possible by setting the option {multipleStatements: true} in your createConnection() function.
So it is like:
mysqlConnection = mysql.createConnection({
host: settings.mysqlServer,
user: settings.mysqlUser,
password: settings.mysqlPassword,
multipleStatements: true
});
mysqlConnection.query( 'CREATE DATABASE NodeTest;USE NodeTest;CREATE TABLE tblUsers (userId varchar(32), userName varchar(255), friendlyUserName varchar(255));' , function( oError ){
if ( oError ) throw oError;
console.log( 'Database created.' );
});