conn.query("insert user set ?",{addr:"湖北",name:"张三"},function(err,result){
if(err)
console.log(err)
console.log(result)
});
上面这个方法只是将{addr:"湖北",name:"张三"},一一对应插入到数据库的 addr 和 name 字段里面。这样很方便不用解析后拼接了。
还可已插入JSON字符串
var mysql=require("mysql");
var conn=mysql.createConnection({
host : "127.0.0.1",
user : "root",
database:"cd",
password: "",
port: "3306"
});
conn.query("insert user set ?",{addr: '{love:"王",why:"I do not know"}' ,name:"陈"},function(err,result){
if(err) //JSON字符串,其实底层还是转义\"
console.log(err)
console.log(result)
});
var mysql=require("mysql");
var conn=mysql.createConnection({
host : "127.0.0.1",
user : "root",
database:"cd",
password: "",
port: "3306"
});
conn.query("select ?? from user where gender = ? ",["gender" , "男"],function(err,result){
if(err) //两个??表示列 一个表示占位符
console.log(err)
console.log(result)
});
conn.query("select * from user where gender = ? and school = ? ",["男",'浙江大学'],function(err,result){
if(err) //一个表示占位符
console.log(err)
console.log(result)
});