The steps are as follows:
1. Set the connection properties to allow multiple SQL statements:
Multiplestatements:true
Const POOL = Mysql.createpool ({ $, ' localhost ', ' root ', ' * * * ', ' DatabaseName ',
DatabaseName do not use uppercase, otherwise the database cannot be found. (Uppercase auto-turn lowercase is supported under windows, only lowercase is supported under Linux) true });
2. Add "SET NAMES ' UTF8" before the SQL statement; "To set the link, platform, result encoding form
3. When the callback function is called, apply (null,arguments) instead of apply (Null,[err,results[1],fields])
//take the Insert data as an examplefunctioninsertsql (table, obj, callback) {Let sqlstr= ""; Let names= []; Let values= []; if(_.isempty (obj)) {sqlstr=' INSERT into ${table} VALUES () '; } Else { for(Let datainchobj) {Names.push (data.tostring ()); Values.push (Obj[data].tostring ()); } sqlstr= ' INSERT into ${table} (${names.join (', ')}) VALUES (${' "' + values.join ('", "') + '" '})`; } console.log (SQLSTR); Pool.getconnection (function(err, connection) {//Use the connectionConnection.query ("SET NAMES ' UTF8 ';" + sqlstr,function(Err, results, fields) {//And done with the connection.connection.release (); //Don ' t use the connection here, it had been returned to the pool.Callback.apply (NULL, [err,results[1],fields]); }); });}
Nodejs+mysql, linking MySQL processing data enforces the use of UTF-8 encoding to avoid garbled characters.