How can I write the following query using template strings of es6?
connection.query('\
CREATE TABLE `' + dbconfig.database + '`.`' + dbconfig.users_table + '` ( \
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, \
`username` VARCHAR(20) NOT NULL, \
`password` CHAR(60) NOT NULL, \
PRIMARY KEY (`id`), \
UNIQUE INDEX `id_UNIQUE` (`id` ASC), \
UNIQUE INDEX `username_UNIQUE` (`username` ASC) \
)');
Is there any other better way of writing multi line sql queries?
So there you have it, multiline query strings have a simple interface in Node.js assuming you are using an ES6 compliant version. If you are familiar with JavaScript, but are just now learning Node.js, I recommend the learning tool known as learnyounode to help you get started.
In this tutorial, You will learn how to execute multiple sql queries using node.js and mysql. To execute multiple SQL statements you need enable multipleStatements option. by default multiple statements is disabled for security reasons. require ('mysql') - Load the mysql module to connect to database.
Is there any other better way of writing multi line sql queries? In ES6 you can use template literals as they're allowed to be multi-line.
Following the GitHub readme for the MySQL library, we can see it’s easy enough to have multiple queries within a single transaction. The above function is basically the same as the previous single transaction by way of: It is not difficult to see that a single transaction to run two queries is rather lengthly.
In ES6 you can use template literals as they're allowed to be multi-line.
connection.query(`CREATE TABLE ${dbconfig.database}.${dbconfig.users_table} (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
password CHAR(60) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX id_UNIQUE (id ASC),
UNIQUE INDEX username_UNIQUE (username ASC)
)`);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With