通过phpmyadmin,NextJS Mysql可以正常运行,但通过api查询时出现问题
P粉549986089
P粉549986089 2023-08-26 16:01:12
0
1
445
<p>如标题所说,我的SQL代码在phpmyadmin中可以工作,但在从API请求时却不行。 我的API使用mysql包:</p> <pre class="brush:php;toolbar:false;">npm i mysql</pre> <p>SQL代码:</p> <pre class="brush:php;toolbar:false;">BEGIN; INSERT INTO Addresses (address, postal_code, city, state, country) VALUES('Something', 69420, 'Something', 'Something', 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_address_id; INSERT INTO Companies (owner, org_nr, name) VALUES('someid', 133769420, 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_company_id; INSERT INTO Users (company, member, administrator) VALUES(@mysql_company_id, 'someid', 1); INSERT INTO CompanyDetails (shippingAddress, company, shortName, ourReference) VALUES(@mysql_address_id, @mysql_company_id, 'Something', 'Something'); COMMIT;</pre> <p>在phpmyadmin中这完全正常。</p> <p>我使用以下方式调用函数:</p>
db.query(
          `开始; INSERT INTO 地址(地址、邮政编码、城市、州、国家) VALUES('Something', 69420, 'Something', 'Something', 'Something');选择 LAST_INSERT_ID() INTO @mysql_address_id; INSERT INTO Companies (owner, org_nr, name) VALUES('Something', 133769420, 'Something');选择 LAST_INSERT_ID() INTO @mysql_company_id; INSERT INTO 用户(公司、成员、管理员)VALUES(@mysql_company_id, 'someid', 1); INSERT INTO CompanyDetails (shippingAddress、company、shortName、ourReference) VALUES(@mysql_address_id, @mysql_company_id, 'Something', 'Something');提交;`,
          (错误,结果)=> {
            if (err) 返回 res.status(422).json(err);
            返回 res.status(200).json(结果);
          }
        );</预>
<p>显示的错误是:</p>
{"code":"ER_PARSE_ERROR","errno":1064,"sqlMessage":"您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在 'INSERT INTO Addresses (address, postal_code, city, state, Country) VALUES('Ve...' at line 1","sqlState" 附近使用的正确语法: "42000","index":0,"sql":"BEGIN; INSERT INTO 地址(地址、邮政编码、城市、州、国家) VALUES('Something', 69420, 'Something', 'Something' , 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_address_id; INSERT INTO Companies (owner, org_nr, name) VALUES('someid', 133769420, 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_company_id; , 成员, 管理员) VALUES(@mysql_company_id, 'someid', 1); INSERT INTO CompanyDetails (shippingAddress, company, ShortName, ourReference) VALUES(@mysql_address_id, @mysql_company_id, 'Something', 'Something'); }</pre>
<p>有人知道这可能是什么原因引起的吗?任何帮助都会不胜感激:)</p>            
P粉549986089
P粉549986089

全部回复(1)
P粉037450467

我在这里找到了解决方案:https://stackoverflow.com/a/23267627/13214923 原来node-mysql默认不支持多个语句。

通过添加

{multipleStatements: true,}

到我的连接池配置中,它就像魔术般地工作了 :)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板