I found what was the problem.
the point is delay. It was a problem that my input was faster than mysql server accepted data.
So I want to close this question and create a new question on how to create a delay in sql foreach
.
It seems weird to me.
First I input the following query in MySQL CLI:
INSERT INTO table1 VALUES (null, 女神在, now(), 0, 0);
It works fine. The data gets inserted as expected.
Then, I input the same query from a Node.js MySQL script.
app.get('/db', (req, res) => {
let var1 = 'english word'
let sql = 'INSERT INTO filelist VALUES (null, ?, ?, ?, ?)';
let addeddate = 'now()';
let isdeleted = '0';
let ismodified = '0';
let params = [var1, addeddate, isdeleted, ismodified];
connection.query(sql, params,
(err, rows, fields) => {
if(err) {
console.log(err);
} else {
res.send(rows);
console.log(rows);
}
}
)
res.render('view');
})
It works fine too, but if I change the value of var1
to Chinese letters, I get the following error:
C:\Users\myapp\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:526:11)
at ServerResponse.header (C:\Users\myapp\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (C:\Users\myapp\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\myapp\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (C:\Users\myapp\node_modules\express\lib\response.js:158:21)
at Query.<anonymous> (C:\Users\myapp\app_file.js:166:21)
at Query.<anonymous> (C:\Users\myapp\node_modules\mysql\lib\Connection.js:526:10)
at Query._callback (C:\Users\myapp\node_modules\mysql\lib\Connection.js:488:16)
at Query.Sequence.end (C:\Users\myapp\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (C:\Users\myapp\node_modules\mysql\lib\protocol\sequences\Query.js:149:8) {
code: 'ERR_HTTP_HEADERS_SENT'
}
Program node app_file.js exited with code 1
res.render
on browser is totally fine. even if var1
is Chinese.
My default character set of db and table is 'utf8_general_ci' of course. I used util.format
or utf8.encode
too, but I got the same error.
I followed Emoji are not inserting in database node js mysql
And I succedeed in inputing "A data" into my table, but the error message still occurs.
I tried again with 'Array', which is what I originally wanted, but it failed.
I found show variables like 'c%';
on google and I typed in mysql cli.
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results
character_set_server latin1
character_set_system utf8
character_sets_dir /rdsdbbin/mysql-5.7.22.R5/share/charsets/
check_proxy_users OFF
collation_connection utf8_general_ci
collation_database utf8_general_ci
collation_server latin1_swedish_ci
completion_type NO_CHAIN
it seems almost every parts are utf8 but server
is latin.
is it critical to me? and then how to change this?