In python, one can use “”” to wrap long MySQL statements. For example,
sql = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )"""
However, if I try the same thing in javascript, there will be syntax error.
connection.query("""CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )""" , function (err, rows, fields) { if (err) throw err; res.send(rows); });
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:
var sql = "CREATE TABLE EMPLOYEE (" + " FIRST_NAME CHAR(20) NOT NULL," + " LAST_NAME CHAR(20)," + " AGE INT," + " SEX CHAR(1)," + " INCOME FLOAT )";
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.