python mysql MySQLdb 存储文件
来源:互联网 发布:淘宝宝贝广告信息违规 编辑:程序博客网 时间:2024/05/22 07:52
再者,在这里我们是想讲一种方法,python+ mysql存储二进制流的方式
这里用的是Mysqldb,python里面最常用的数据库模块
import MySQLdb class BlobDataTestor: def __init__ (self): self.conn = MySQLdb.connect(host='localhost',user='',passwd='',db='0') def __del__ (self): try: self.conn.close() except : pass def closedb(self): self.conn.close() def setup(self): cursor = self.conn.cursor() cursor.execute( """ CREATE TABLE IF NOT EXISTS `Dem_Picture` ( `ID` int(11) NOT NULL auto_increment, `PicData` mediumblob, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; """) def teardown(self): cursor = self.conn.cursor() try: cursor.execute( "Drop Table Dem_Picture" ) except: pass # self.conn.commit() def testRWBlobData(self): # 读取源图片数据 f = open( "C:\\11.jpg" , "rb" ) b = f.read() f.close() # 将图片数据写入表 cursor = self.conn.cursor() cursor.execute( "INSERT INTO Dem_Picture (PicData) VALUES (%s)" , (MySQLdb.Binary(b))) # self.conn.commit() # 读取表内图片数据,并写入硬盘文件 cursor.execute( "SELECT PicData FROM Dem_Picture ORDER BY ID DESC limit 1" ) d = cursor.fetchone()[0] cursor.close() f = open( "C:\\22.jpg" , "wb" ) f.write(d) f.close() if __name__ == "__main__": test = BlobDataTestor() try: test.setup() test.testRWBlobData() test.teardown() finally: test.closedb()
到这里python mysql存储二进制图片的方法就将完了
0 0
- python mysql MySQLdb 存储文件
- Python使用MySQLdb创建、调用MySQL存储过程
- mysql-python MySQLdb
- Python连接MYSQL --- MySQLdb
- Python MySQL(MySQLdb)
- python mysql MySQLdb
- python之mysql连接 MySQLdb
- Python使用MySQLdb操作MySQL
- MySQL and Python's MySQLdb
- python操作MySQL数据库(MySQLdb
- Linux + python +mysqlDB + mysql + innoDB
- python通过MySQLdb操纵mysql
- MySQLdb:Python 操作 MySQL 数据库
- ubuntu 安装MySQL-python(MySQLdb)
- python-mysql安装mysqldb模块
- python 通过mysqldb 操作mysql数据库
- python中利用MySQLdb操作mysql(二)
- python中使用MySQLdb连接mysql
- linux GPIO中断的使用笔记
- 一笔画问题
- CheckBox样式
- 设A和B是两个单链表,其表中元素递增有序,使用一算法将A和B归并成一个案元素递减有序的单链表C(该程序中的功能包含了上几个博客中的部分功能)
- nginx http 411 length required问题
- python mysql MySQLdb 存储文件
- 我是如何自学Android,资料分享(2015 版)
- cocos2d js键盘按键相关
- secilog 1.15 发布 增加了搜索保存数据库采集web日志报表等。
- Universal Image Loader 笔记
- springMVC原理
- stvd watch 实时变量查看
- QQ,微博,微信链接分享无法显示分享记录
- MyBatis入门06 动态sql语句