Repository navigation
[問題] MySQLdb模組問題 #2878
Unanswered
LeoTsai999
asked this question in
Q&A
Replies: 1 comment
|
Thanks @Sagargupta16! Great tips. The .cursorrules file I proposed actually encodes a lot of these patterns — like the async/await best practices, Pydantic model patterns, and TestClient usage — so Cursor IDE can auto-suggest them while you code. The idea is that instead of memorizing all the FastAPI conventions, your IDE just knows them. For example, it'll suggest If anyone wants to try it out, the rules file is in the discussion above. Would love feedback on which patterns are most useful to include. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
這是在ptt上看到的一篇關於python的小問題,覺得很有趣,想和大家分享一下。
hi 各位前輩
想請教一下
目前在使用MySQLdb模組遇到瓶頸
在進入DB後
sql = select * from %s where aaa = %s
cursor.execute(sql, (a, b))
由於b為string , 且內容含特殊字元
所以在查詢DB時
cursors.py會自動幫我加上\
原本b
ftp://www.abc.com/['123']
送出會變成
ftp://www.abc.com/[\\'123\\']
但是我要的結果需保留單引號
所以手動加入跳脫字元'
把b改成
ftp://www.abc.com/[\'123\']
但送出結果卻變成
ftp://www.abc.com/[\\\\\\'123']
而且更怪的是
若改成下面
sql = select * from table where aaa = %s
cursor.execute(sql, (b))
這樣居然會執行正確
而不會加上\
請問有經驗的大大可解惑嗎?
感謝
--
在使用 MySQLdb 模組時,建議避免手動處理跳脫字元,因為這可能導致不必要的錯誤。你的問題源於對 SQL 查詢值的處理方式。在使用
cursor.execute(sql, (b,))時,b會被正確地作為參數處理,避免了自動加上多餘的跳脫字元。對於特殊字元,你可以考慮使用 UTF-8 編碼或其他字符處理方式,這樣可以確保數據正確傳送至資料庫。同時,熟悉 Python 的
parameterized queries可以讓你避免 SQL 注入的風險,並且更方便處理各種數據類型。建議學習 SQLAlchemy 等 ORM 框架,這些工具會更有效率地處理資料庫交互並隱藏底層細節,提升開發體驗。
All reactions