顯示具有 python 標籤的文章。 顯示所有文章
顯示具有 python 標籤的文章。 顯示所有文章

利用 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])+"'")

python 在安裝 MysqlDB 時,出現找不到 mysql_config 的錯誤

使用 python 在操作 Mysql 時,會需要用到 MySQLdb 這個 library,如果你在安裝時,出現找不到 mysql_config 的錯誤,可能的原因是當初在安裝 Mysql 時,是使用 apt-get 的方法安裝,這樣就不會有 mysql_config 這個檔案。

解決的方式是安裝 dev 版本,分別執行下面的指令:

這樣就解決了。