Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write multi line sql query (nodejs)

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?

like image 255
Soli Avatar asked Dec 31 '17 17:12

Soli


People also ask

Do multiline query strings have an interface in Node JS?

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.

How to execute multiple SQL queries using Node JS and MySQL?

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 way of writing multi line SQL queries?

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.

Is it possible to have multiple queries in a single transaction?

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.


1 Answers

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) 
)`);
like image 119
Flying Avatar answered Oct 19 '22 20:10

Flying