In python, one can use “”” to wrap long MySQL statements. For example,
JavaScript
x
7
1
sql = """CREATE TABLE EMPLOYEE (
2
FIRST_NAME CHAR(20) NOT NULL,
3
LAST_NAME CHAR(20),
4
AGE INT,
5
SEX CHAR(1),
6
INCOME FLOAT )"""
7
However, if I try the same thing in javascript, there will be syntax error.
JavaScript
1
13
13
1
connection.query("""CREATE TABLE EMPLOYEE (
2
FIRST_NAME CHAR(20) NOT NULL,
3
LAST_NAME CHAR(20),
4
AGE INT,
5
SEX CHAR(1),
6
INCOME FLOAT )"""
7
8
9
, function (err, rows, fields) {
10
if (err) throw err;
11
res.send(rows);
12
});
13
Is there some kind of javascript equivalent for python’s """
string encapsulation? If no, what are some best practices for encapsulating a long MySQL string statement in javascript?
I am using node.js restify client.
Advertisement
Answer
Dealing with long strings in JavaScript:
JavaScript
1
7
1
var sql = "CREATE TABLE EMPLOYEE (" +
2
" FIRST_NAME CHAR(20) NOT NULL," +
3
" LAST_NAME CHAR(20)," +
4
" AGE INT," +
5
" SEX CHAR(1)," +
6
" INCOME FLOAT )";
7
Python’s triple quotes are great! Unfortunately, in JavaScript, you have only two options:
+
based concatenation, as abovebased continuation, as proposed by @Nina Scholz
Personally, I don’t like using for line continuation (in any language.) Using
+
doesn’t introduce unnecessary spaces in your string either.
Hope this helps.