python+ mysql 存储二进制流的简单示例

python+ mysql 存储二进制流的简单示例,第1张

概述python+ mysql 存储二进制流的简单示例 对python这个高级语言感兴趣的小伙伴,下面一起跟随内存溢出 jb51.cc的小编两巴掌来看看吧!

很多时候我们为了管理方便会把依稀很小的图片存入数据库,有人可能会想这样会不会对数据库造成很大的压力,其实大家可以不用担心,因为我说过了,是存储一些很小的图片,几K的,没有问题的!

再者,在这里我们是想讲一种方法,python+ MysqL存储二进制流的方式

这里用的是MysqLdb,python里面最常用的数据库模块
 

# @param python+ MysqL存储二进制流的方式# @author 内存溢出 jb51.cc|www.www.jb51.cc 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()# End www.jb51.cc

到这里python MysqL存储二进制图片的方法就将完了

总结

以上是内存溢出为你收集整理的python+ mysql 存储二进制流的简单示例全部内容,希望文章能够帮你解决python+ mysql 存储二进制流的简单示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1199901.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存