<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() INTO9420, 'Something'); SELECT ,管理員)VALUES(@mysql_company_id,'someid',1); INSERT INTO CompanyDetails(shippingAddress,company,shortName,ourReference)VALUES(@mysql_address_id,@mysql_company_id,'Something','Somethingthing'); ;/pre>
<p>有人知道這可能是什麼原因引起的嗎?任何幫助都會不勝感激:)</p>
我在這裡找到了解決方案:https://stackoverflow.com/a/23267627/13214923 原來node-mysql預設不支援多個語句。
透過新增
到我的連接池配置中,它就像魔術般地工作了 :)