I have this query:
q="INSERT INTO customers (?) VALUES (?)"
and I'm trying to pass the first placeholder an array of column names and the second placeholder an array of values, like so:
columns_arr = [
'stat',
'idcustomers',
'parent_id',
'cust_prio_code',
'custadd_type_code',
'customername',
'EMAIL',
'priority_id'
]
values_arr = [
2,
300,
900,
10003999,
'someType',
'dave',
'[email protected]',
99
]
var inserts = [columns_arr, values_arr]
var format_query = mysql.format(q, inserts)
mydb.query(format_query)
but the query that ends up being executed is:
INSERT INTO `customers` ('stat', 'idcustomers', 'parent_id', 'cust_prio_code', 'custadd_type_code', 'customername', 'EMAIL', 'priority_id') VALUES (2, 300, 900, 10003999, 'someType', 'dave', '[email protected]', 99)
and it gives a syntax error:
(node:18776) UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''stat', 'idcustomers', 'parent_id', 'cust_prio_code', 'custadd_type_code', 'c...' at line 1
So, column names are being passed as strings. How can I solve it? I get this array dynamically, as it is, and so I can't just manually change it to an array without quotes, and even if I could it wouldn't compile as these are not declared in the code. trying to use replace(/'/g,'') didn't help. How can I pass this array to the query without the single quotes on each column name?
and if it's not the problem then what is?
Thanks a lot
INSERT INTO customers ? VALUES ?