gpt4 book ai didi

mysql - 从 js 运行时插入错误,xampp shell 使用相同的 sql 工作正常

转载 作者:行者123 更新时间:2023-11-29 15:35:47 25 4
gpt4 key购买 nike

这是我尝试运行的sql,当我使用xampp时,我能够得到插入表的响应。但如果我将 sql 传递到查询函数中,我无法将查询插入到我的表中。我收到以下错误:

START TRANSACTION;
INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_county, address_postcode) VALUES ('place', '', 'Town', 'county', 'postcode');
SELECT LAST_INSERT_ID();
COMMIT;

使用js:

var query= function (sql){
return new Promise(resolve => {
con.query(sql, (err, result) => {
if(err) throw err;
resolve(result);
});
});
}

错误:

C:\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_count' at line 1
at Query.Sequence._packetToError (C:\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Query.ErrorPacket (C:\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (C:\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (C:\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (C:\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\node_modules\mysql\lib\Connection.js:91:28)
at Socket.<anonymous> (C:\node_modules\mysql\lib\Connection.js:525:10)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
--------------------
at Protocol._enqueue (C:\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\node_modules\mysql\lib\Connection.js:201:25)
at resolve (C:\db.js:18:13)
at new Promise (<anonymous>)
at Object.query (C:\db.js:17:12)
at insert (C:\utl\order_model.js:16:8)
at Order.insert (C:\utl\order_model.js:99:9)
at utill.calculate_cart (C:\routes\orderRouter.js:65:15)
at pm.getProducts.productlist (C:\utl\utill.js:22:9)
at Object.getProducts (C:\utl\products_model.js:27:9)
[nodemon] app crashed - waiting for file changes before starting...

最佳答案

因此,只需阅读错误消息并与上面的(工作)查询进行比较即可:

Working:         INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_county, address_postcode) VALUES ('place', '', 'Town', 'county', 'postcode');

Error Message: 'INSERT INTO address_tbl (address_ln_1, address_ln_2, address_town, address_count'

您缺少表格插入的一些参数。我会看看这是为什么。

关于mysql - 从 js 运行时插入错误,xampp shell 使用相同的 sql 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252498/

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