HOw can i send null value to database in node.js? I am using MySql Package for node JS
我可以在node.js中向数据库发送空值吗?我正在使用MySql Package作为节点JS
and I have some null values to insert which I want to be stored in database as DBNull.Value( as in asp.net) and not as "null" or " ".
我有一些空值要插入我希望作为DBNull.Value存储在数据库中(如在asp.net中)而不是“null”或“”。
Also, I cannot skip any value to insert.
此外,我不能跳过任何值插入。
My code below:
我的代码如下:
var query=connection.query("insert into user_details(user_id,username,screenname,firstname,middlename,lastname,about_me,color_code,Gender,hobbies,occupation,member_of_group,profile_pic,address1,address2,city,pincode,phonenumber,EmailId1,EmailId2,createdate,updatedate) values('"+json.user_id+"','"+ json.username+"','"+json.screenname+"','"+json.firstname+"','"+json.middlename+"','"+json.lastname+"','"+json.about_me+"','"+json.color_code+"','"+json.Gender+"','"+json.hobbies+"','"+json.occupation+"','"+json.member_of_group+"','"+json.profile_pic+"','"+json.address1+"','"+json.address2+"','"+json.city+"',"+json.pincode+",'"+json.phonenumber+"','"+json.EmailId1+"','"+json.EmailId2+"','"+json.createdate+"','"+json.updatedate+"');",function(err,result){
if(!err){
connection.destroy();
callback(result);
}
else
{
console.log(err);
callback('error');
}
});
How can I do that???
我怎样才能做到这一点???
1 个解决方案
#1
3
First of all, you shouldn't directly concatenate (especially user submitted) values for security reasons and it's just plain tedious when you have a lot of concatenation going on.
首先,出于安全原因,您不应该直接连接(尤其是用户提交的)值,并且当您进行大量连接时,这只是简单的单调乏味。
Try this instead which makes it easier to work with and properly escapes your values:
尝试使用此功能可以更轻松地使用并正确转义您的值:
connection.query('INSERT INTO user_details SET ?', json, function(err, result) {
connection.destroy();
if (err)
console.log(err);
callback(err, result);
});
or manually specify the values if the keys in the data source do not match up with column names:
或者,如果数据源中的键与列名称不匹配,请手动指定值:
var values = {
user_id: json.id,
username: json.user,
// ...
};
connection.query('INSERT INTO user_details SET ?', values, function(err, result) {
connection.destroy();
if (err)
console.log(err);
callback(err, result);
});
Secondly, typically callbacks use "error-first", so passing a real error object as the first argument is better.
其次,通常回调使用“error-first”,因此传递真实的错误对象作为第一个参数更好。
#1
3
First of all, you shouldn't directly concatenate (especially user submitted) values for security reasons and it's just plain tedious when you have a lot of concatenation going on.
首先,出于安全原因,您不应该直接连接(尤其是用户提交的)值,并且当您进行大量连接时,这只是简单的单调乏味。
Try this instead which makes it easier to work with and properly escapes your values:
尝试使用此功能可以更轻松地使用并正确转义您的值:
connection.query('INSERT INTO user_details SET ?', json, function(err, result) {
connection.destroy();
if (err)
console.log(err);
callback(err, result);
});
or manually specify the values if the keys in the data source do not match up with column names:
或者,如果数据源中的键与列名称不匹配,请手动指定值:
var values = {
user_id: json.id,
username: json.user,
// ...
};
connection.query('INSERT INTO user_details SET ?', values, function(err, result) {
connection.destroy();
if (err)
console.log(err);
callback(err, result);
});
Secondly, typically callbacks use "error-first", so passing a real error object as the first argument is better.
其次,通常回调使用“error-first”,因此传递真实的错误对象作为第一个参数更好。