在 javascript 中包装长 SQL 语句的便捷方法 [英] Convenient way to wrap long SQL statements in javascript

查看:51
本文介绍了在 javascript 中包装长 SQL 语句的便捷方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,可以使用"""来包装长MySQL语句.例如,

sql = """CREATE TABLE EMPLOYEE (FIRST_NAME CHAR(20) 非空,LAST_NAME 字符(20),年龄 INT,性别特征(1),收入浮动 )"""

但是,如果我在 javascript 中尝试相同的操作,则会出现语法错误.

connection.query("""CREATE TABLE EMPLOYEE (FIRST_NAME CHAR(20) 非空,LAST_NAME 字符(20),年龄 INT,性别特征(1),收入浮动 )""",函数(错误,行,字段){如果(错误)抛出错误;res.send(行);});

python 的 """ 字符串封装是否有某种 javascript 等价物?如果没有,在 javascript 中封装长 MySQL 字符串语句的最佳实践是什么?

我正在使用 node.js restify 客户端.

解决方案

在 JavaScript 中处理长字符串:

var sql = "CREATE TABLE EMPLOYEE (" +" FIRST_NAME CHAR(20) NOT NULL," +" LAST_NAME CHAR(20)," +" 年龄 INT," +" 性别字符(1)," +" 收入浮动 )";

Python 的三重引号很棒!不幸的是,在 JavaScript 中,您只有两个选择:

    基于
  • + 的串联,同上
  • \ 基于continuation,由@Nina Scholz 提出

就我个人而言,我不喜欢将 \ 用于行继续(在任何语言中).使用 + 也不会在您的字符串中引入不必要的空格.

希望这会有所帮助.

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.

解决方案

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 above
  • \ based 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.

这篇关于在 javascript 中包装长 SQL 语句的便捷方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆