gpt4 book ai didi

mysql - 发生 ER_PARSE_ERROR

转载 作者:行者123 更新时间:2023-11-29 17:25:54 31 4
gpt4 key购买 nike

我想做网页抓取。我写了代码

var connection = require('./mysqlConnection'); 
var c = new Crawler({
maxConnections : 10,
callback : function (error, result, $) {
if(error){
console.log(error);
}else{
const data = $(".test");
for(var i = 0; i < data.length; i ++){
const item = $(data[i]).text();
var query = 'INSERT INTO crawling (item) VALUES ("' + item + '")';
connection.query(query, function(err, rows) {
console.log(err);
});
}
}
}
});
c.queue('https://xxxxxxxxxx.com');

当我运行此代码时,const item 可以在中途获取,但是

{ 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 'June18th
")' at line 1
at Query.Sequence._packetToError (/Users/xxx/xxx/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Query.ErrorPacket (/Users/xxx/xxx/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
at Protocol._parsePacket (/Users/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (/Users/xxx/xxx/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/Users/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/Users/xxx/xxx/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
--------------------
at Protocol._enqueue (/Users/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Connection.query (/Users/xxx/xxx/node_modules/mysql/lib/Connection.js:208:25)
at Object.callback (/Users/xxx/xxx/app.js:135:24)
at Crawler._onInject (/Users/xxx/node_modules/node-webcrawler/lib/crawler.js:428:13)
at /Users/xxx/node_modules/node-webcrawler/lib/crawler.js:395:11
at Crawler._inject (/Users/xxx/node_modules/node-webcrawler/lib/crawler.js:154:9)
at Crawler._onContent (/Users/xxx/node_modules/node-webcrawler/lib/crawler.js:394:7)
at Request._callback (/Users/xxx/node_modules/node-webcrawler/lib/crawler.js:332:14)
at Request.self.callback (/Users/xxx/node_modules/node-webcrawler/node_modules/request/request.js:187:22)
at emitTwo (events.js:126:13)

code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: '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 \'June18th"\n \n \n ")\' at line 1',
sqlState: '42000',
index: 0,
sql: 'INSERT INTO crawling (title) VALUES (" \n I went to Paris at"June18th"because\n \n \n ")' }

我真的不明白为什么会发生这样的错误。我认为\n 导致了这个错误,所以我重写了

const item = $(data[i]).text().replace("\n", '');

但是发生了同样的错误。我的代码出了什么问题?我应该如何解决这个问题?

最佳答案

尝试更改:connection.query(query, function(err, rows) {

connection.query([query], function(err, rows) {

它周围可能需要[]

关于mysql - 发生 ER_PARSE_ERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50971502/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com