problem description
on the basis of the general query connection.query (querySql,values,function () {}) of the mysql module in NODE.JS, how to abstract the mysql dynamic parameter query (no, one, multiple parameters)?
related codes
has unconditional parameters selectAll ()
has query ("SELECT * FROM TABLE", null,callback);
has a conditional parameter select (values)
expected and final result: "SELECT * FROM TABLE"
there is query ("SELECT * FROM TABLE WHERE?", values,callback);
where general values= {age: 23};
expected and final result: "SELECT * FROM TABLE WHERE age=23"
preliminary attempt
integrate unconditional parameters into one conditional parameter
query ("SELECT * FROM TABLE WHERE?, values,callback);
where general values= {1: 1};
the expected result is
"SELECT * FROM TABLE WHERE 1: 1;
but the error message will be reported. The error message actually seen
Error: ER_BAD_FIELD_ERROR: Unknown column"1" in "where clause"
it queried the first 1 as a column and reported an error.
other attempts
does multiple conditional parameters look like this? Select (values)
query ("SELECT * FROM TABLE WHERE?", values,callback);
if there is values= {age: 23 age gender: "male"};
the expected result is
"SELECT * FROM TABLE WHERE age=23 and gender:" male";
but the error message will be reported. The error message actually seen
check the manual that corresponds to your MySQL server version for the right syntax to use near" gender
= "male"" at line 1;
in addition, my attempt
if values= [{age: 23}, {gender: "male"}];
outputs the same result as values= {age: 23}. The method ignores the second conditional parameter.
(this parameter form values= [{age: 23}, {gender: "male"}];
in query (" UPDATE TABLE SET? WHERE?", values,callback); is available)
other attempts
query ("SELECT * FROM TABLE WHERE? AND?", values,callback);
values= [{age: 23}, {gender: "male"}];
this is successful, but it all looks silly, because they are all query statements, nothing more than the number of parameters, is there a better way to integrate them