|
實(shí)例7、把圖片用二進(jìn)制存入MYSQL 首先,在數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)表,用于存放圖片: 復(fù)制代碼 代碼如下: CREATE TABLE Images(Id INT PRIMARY KEY AUTO_INCREMENT, Data MEDIUMBLOB); 然后運(yùn)行如下PYTHON代碼進(jìn)行: 復(fù)制代碼 代碼如下: # -*- coding: UTF-8 -*- # 來(lái)源:瘋狂的螞蟻的博客總結(jié)整理 import MySQLdb as mdb import sys try: except IOError, e: try: #提交數(shù)據(jù) #提交之后,再關(guān)閉cursor和鏈接 except mdb.Error, e: 結(jié)果:
實(shí)例8、從數(shù)據(jù)庫(kù)中把圖片讀出來(lái) 復(fù)制代碼 代碼如下: # -*- coding: UTF-8 -*- # 來(lái)源:瘋狂的螞蟻的博客總結(jié)整理 import MySQLdb as mdb import sys try: cursor = conn.cursor() #執(zhí)行查詢?cè)搱D片字段的SQL #使用二進(jìn)制寫(xiě)文件的方法,打開(kāi)一個(gè)圖片文件,若不存在則自動(dòng)創(chuàng)建 #釋放查詢數(shù)據(jù)的資源 except IOError, e: -------------------------------------------------------------------------------------- 實(shí)例9、使用Transaction即事務(wù)(手動(dòng)提交,自動(dòng)回滾) 復(fù)制代碼 代碼如下: # -*- coding: UTF-8 -*- # 來(lái)源:瘋狂的螞蟻的博客總結(jié)整理 import MySQLdb as mdb import sys try: cursor = conn.cursor() #事務(wù)的特性1、原子性的手動(dòng)提交 cursor.close() except mdb.Error, e: 來(lái)自:http://www.jb51.net/article/34102.htm |
|
|
來(lái)自: java_laq小館 > 《Python》