![](/img/trans.png)
[英]Can python sqlalchemy use a variable to construct an insert statement (for mysql database)
[英]Fix python Sqlalchemy model can not insert mysql syntax error
Python模型:
class SYSLocation(SYSModel):
__tablename__ = 'sys_location'
rank = db.Column(db.Integer)
呼叫:db.session.add(model)
它生成mysql腳本:
INSERT INTO `sys_location` ( rank) VALUES (13000)
sql錯誤:
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 'rank)
VALUES (13000)' at line 1
我檢查此查詢運行錯誤mysql版本8.0.11,因為等級是關鍵字。 但是此sql可以運行mysql版本10.1.25-MariaDB 。 如何修復我的Sqlalchemy模型運行所有版本的mysql?
與往常一樣,當處理保留關鍵字時,像處理sys_location
一樣將其包裝在反引號中:
INSERT INTO `sys_location` (`rank`) VALUES (13000)
您可以根據需要逃避一切,但這通常不是必需的。 關鍵字是一種必要的情況,因為這些關鍵字的確會偶爾更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.