新建表img,设置id字段和imgs字段(设置为blob属性,二进制数据)
-
#!/usr/bin/env python
-
# -*- coding: utf-8 -*-
-
# @Time : 2017/8/18 11:37
-
# @File : Picture.py
-
import MySQLdb as mysql
-
import sys
-
-
conn=mysql.connect(host= 'localhost',user= 'root',passwd= 'xxx',db= 'mydata')
-
-
fp = open( "./1.jpg")
-
img = fp.read()
-
fp.close()
-
-
-
#存入图片
-
def insert_imgs(img):
-
# mysql连接
-
-
cursor = conn.cursor()
-
# 注意使用Binary()函数来指定存储的是二进制
-
# cursor.execute("insert into img set imgs='%s'" % mysql.Binary(img))
-
cursor.execute( "Insert into img(imgs) values(%s)", (mysql.Binary(img)))
-
# 如果数据库没有设置自动提交,这里要提交一下
-
conn.commit()
-
cursor.close()
-
# 关闭数据库连接
-
conn.close()
-
-
-
#提取图片
-
def select_imgs(img):
-
cursor=conn.cursor()
-
cursor.execute( 'select imgs from img')
-
print cursor.fetchall()
-
cursor.close()
-
conn.close()