我正在使用 npm 模块mysql2
这是代码:
var values = [['a', 'b', 'c', 'd', 'https://ne.wikipedia.org/wiki/%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4%AF_%E0']];
con.query(
"INSERT INTO urlparse (title, description, author, keywords, url) VALUES ?",
[values],
function(err, result) {
if (err) throw err;
}
);
});
这是我得到的错误:
^
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 ''https://ne.wikipedia.org/wiki/%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4%AF_%E0' at line 1
at Packet.asError (/rbd/pnpm-volume/12c7da94-52ca-491c-a99d-4962bc1f1e80/node_modules/mysql2/lib/packets/packet.js:712:17)
at Query.execute (/rbd/pnpm-volume/12c7da94-52ca-491c-a99d-4962bc1f1e80/node_modules/mysql2/lib/commands/command.js:28:26)
at Connection.handlePacket (/rbd/pnpm-volume/12c7da94-52ca-491c-a99d-4962bc1f1e80/node_modules/mysql2/lib/connection.js:417:32)
at PacketParser.onPacket (/rbd/pnpm-volume/12c7da94-52ca-491c-a99d-4962bc1f1e80/node_modules/mysql2/lib/connection.js:75:12)
at PacketParser.executeStart (/rbd/pnpm-volume/12c7da94-52ca-491c-a99d-4962bc1f1e80/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/rbd/pnpm-volume/12c7da94-52ca-491c-a99d-4962bc1f1e80/node_modules/mysql2/lib/connection.js:82:25)
at Socket.emit (events.js:196:13)
at addChunk (_stream_readable.js:290:12)
at readableAddChunk (_stream_readable.js:271:11)
at Socket.Readable.push (_stream_readable.js:226:10)
at TCP.onStreamRead (internal/stream_base_commons.js:166:17)
请帮忙。
您正在为查询中的值传递数组数组 尝试使用values
而不是[values]
var values = ['a', 'b', 'c', 'd',
'https://ne.wikipedia.org/wiki/%E0%A4%AE%E0%A5%81%E0%A4%96%E0%A5%8D%E0%A4%AF_%E0'];
con.query(
"INSERT INTO urlparse (title, description, author, keywords, url) VALUES (?, ?, ?, ?, ?)",
values, // Array of values here
function(err, result) {
if (err) throw err;
}
);