使用变量在sync-mysql节点js中调用查询

时间:2018-07-05 05:17:30

标签: mysql node.js

var mysql= require('sync-mysql');
var connection = new mysql({
    host: 'host-name',
    user: 'root',
    password: 'password',
    database: 'tables', //name of the database
    multipleStatements: true
});

var val="value"
const ans= connection.queueQuery("Select * from table Where col_1=val";);
console.log(ans());

尽管存在用于此类列的元组,但查询返回的是空数组。 如何在查询中使用局部变量?

1 个答案:

答案 0 :(得分:0)

您可以使用ES6 Template literals:

const ans = connection.queueQuery(`Select * from table Where col_1 = ${val}`);

或简单的串联:

const ans = connection.queueQuery('Select * from table Where col_1 = ' + val);