python将图片以二进制存入mysql并取出来
2013-04-22 21:58
591 查看
import MySQLdb as mdb
class BlobData:
def __init__(self):
self.conn = mdb.Connect('localhost', 'root', '123456', 'test')
def __del__(self):
try:
self.conn.close()
except:
print "close database error"
def closedb(self):
self.conn.close()
def setup(self):
cursor = self.conn.cursor()
try:
cursor.execute("Create table if not exists picture(id int(3) primary key auto_increment, pic_name varchar(20), data longblob) engine=MyISAM default charset = utf8;")
except Exception, e:
print "create database error:", e
finally:
cursor.close()
def teardown(self):
cursor = self.conn.cursor()
try:
cursor.execute("drop table picture")
except Exception, e:
print "drop database error", e
finally:
cursor.close()
def testRWBlobData(self):
fil = open("c:/web.jpg", 'rb')
b = fil.read()
fil.close()
cursor = self.conn.cursor()
cursor.execute("Insert into picture(data) values(%s)", (mdb.Binary(b)))
cursor.execute("select data from picture order by id desc limit 1")
d = cursor.fetchone()[0]
cursor.close()
f = open("c:/images.jpg", 'wb')
f.write(d)
f.close()
if __name__ == "__main__":
test = BlobData()
try:
test.setup()
test.testRWBlobData()
test.teardown()
finally:
test.closedb()
class BlobData:
def __init__(self):
self.conn = mdb.Connect('localhost', 'root', '123456', 'test')
def __del__(self):
try:
self.conn.close()
except:
print "close database error"
def closedb(self):
self.conn.close()
def setup(self):
cursor = self.conn.cursor()
try:
cursor.execute("Create table if not exists picture(id int(3) primary key auto_increment, pic_name varchar(20), data longblob) engine=MyISAM default charset = utf8;")
except Exception, e:
print "create database error:", e
finally:
cursor.close()
def teardown(self):
cursor = self.conn.cursor()
try:
cursor.execute("drop table picture")
except Exception, e:
print "drop database error", e
finally:
cursor.close()
def testRWBlobData(self):
fil = open("c:/web.jpg", 'rb')
b = fil.read()
fil.close()
cursor = self.conn.cursor()
cursor.execute("Insert into picture(data) values(%s)", (mdb.Binary(b)))
cursor.execute("select data from picture order by id desc limit 1")
d = cursor.fetchone()[0]
cursor.close()
f = open("c:/images.jpg", 'wb')
f.write(d)
f.close()
if __name__ == "__main__":
test = BlobData()
try:
test.setup()
test.testRWBlobData()
test.teardown()
finally:
test.closedb()
相关文章推荐
- python3图片转换二进制存入mysql
- python3图片转换二进制存入mysql
- 图片 二进制 存入 mysql
- 图片以二进制存入MYSQL
- asp.net FileUpload 控件上传文件 以二进制的形式存入数据库并将图片显示出来
- Instance Seven:把图片用二进制存入MYSQL
- 图片二进制存入数据库
- PHP将图片存入MYSQL
- java 上传图片 二进制保存到 mysql 请求图片二进制转化为图片 cxf rest jax-rs
- ASP.NET实现图片以二进制的形式存入数据库
- 二进制数据 + MySQL + PHP 怎样在Mysql中直接储存图片
- 用Python实现B2M算法:将二进制文件映射为灰度图片
- Win form 在pictureBox控件中实现选择图片然后以二进制方式保存到数据库。以及在pictureBox控件显示出来
- python爬取网页公告并批量存入至mysql中
- python 笔记 在windows下的程序 获取图片并显示出来
- C#把图片以二进制形式存入数据库
- 用Java 将图片存入Mysql 并读出的方法
- python中,从mysql读取数据,并存入redis里面(1)
- python 读取二进制 显示图片
- python获取mysql天数据,聚合存入mongodb(我的第一个用python写的程序)