Hus*_*sky 2 mysql node.js node-mysql
Node.js,MySQL(使用
node-mysql library连接)
我可以有one argument 'aaa'或two arguments 'aaa' and 'bbb'。
UPDATE user SET email = 'aaa' WHERE uid = 'xxx';
UPDATE user SET email = 'aaa', password = 'bbb' WHERE uid = 'xxx';
Run Code Online (Sandbox Code Playgroud)
如何使用一个查询来克服上述情况?像下面...
var sql = "UPDATE user SET email = ?, password = ? WHERE uid = ?;"
dbclient.query(sql, [email, password, uid], function (err, results) {
...
});
Run Code Online (Sandbox Code Playgroud)
如果有两个参数defined,则变量sql为:
sql = "UPDATE user SET email = 'test@mail.com', password = 'abcd' WHERE uid = 1;";
Run Code Online (Sandbox Code Playgroud)如果有人参数为undefined,则变量sql为:
sql = "UPDATE user SET email = 'test@mail.com', password = password WHERE uid = 1;";
Run Code Online (Sandbox Code Playgroud)请阅读文档,尤其是转义查询值的部分。node-mysql支持对象序列化的一些巧妙使用。
以下示例应为您想要的示例(它使用格式,但query()内部使用格式):
var mysql = require( 'mysql' );
var credfull = {
email: "a@exemple.com",
password: "secret"
},
crednopwd = {
email: "a@exemple.com"
},
whereclause = {
uid: "blabla"
};
console.log( mysql.format( 'UPDATE user SET ? WHERE ?', [credfull, whereclause]) );
// UPDATE user SET `email` = 'a@exemple.com', `password` = 'secret' WHERE `uid` = 'blabla'
console.log( mysql.format( 'UPDATE user SET ? WHERE ?', [crednopwd, whereclause]) );
// UPDATE user SET `email` = 'a@exemple.com' WHERE `uid` = 'blabla'
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
1775 次 |
| 最近记录: |