0

I am trying to do a bulk insert, and even followed the code found here: Stackoverflow Question

it only inserts the first row not sure why? Using nodejs with mysql.

var ethnicity_interested = [1, 2, 3];
var newEthArr = [];

ethnicity_interested.forEach(function(i) {
                        newEthArr.push([i, userObj.id])
                    })
                    var fields = "INSERT INTO User_Pref_Ethnicity (ethnicity_id, user_id) VALUES (?)";
                    var inserts = [
                        newEthArr
                    ];
                    var sqlNewUser = mysql.format(fields, newEthArr);
                    connection.query(sqlNewUser, function(
                        error,
                        results,
                        fields
                    ) {
                        if (error) {
                            console.log("insert  User_Pref_Ethnicity  err: " + error);
                            res.json({
                                http_code: 500,
                                error_message: 'Server Error: Please Try Again.'
                            });
                        } else {
                            storeUserPrefReligion();
                        }
                    });

I even tried to use the version in the example like so:

var fields = "INSERT INTO User_Pref_Ethnicity (ethnicity_id, user_id) VALUES ?";
                    var inserts = [
                        newEthArr
                    ];
                    ///var sqlNewUser = mysql.format(fields, newEthArr);
                    connection.query(fields, newEthArr, function(

and I get this error:

insert  User_Pref_Ethnicity  err: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2, 2' at line 1
Lion789
  • 4,402
  • 12
  • 58
  • 96
  • The example you say you're following doesn't use `mysql.format()`. And it says to use `VALUES ?` not `VALUES (?)`. What happens if you follow the example more precisely? – O. Jones Oct 13 '20 at 00:03
  • I removed the parenthesis and then the format, and now I get this error insert User_Pref_Ethnicity err: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2, 2' at line 1 – Lion789 Oct 13 '20 at 00:07
  • sir @Lion789 , able to solve this problem? I also received same problem. – khoi Sep 12 '22 at 05:08

0 Answers0