利用 python update 資料庫欄位,跳脫字元的方式 (escape string)

在 python 中,用 Mysqldb module 結果在 stackoverflow 一個討論串中有個很讚的做法:

What is the best escape character strategy for Python/MySQL combo?
  • row[1] 指的是 column name
  • row[3] 指的是要 insert 的 data
  • row[0] 指的是 where 條件式域比對的限制條件

比對一下原始的 sql 就很清楚了:
cursor2.execute("update myTable set `"+ str(row[1]) +"` = \"'" + str(row[3]) +"'\" where ID = '"+str(row[0])+"'")

Share this post!

Bookmark and Share

0 意見: