python 往mysql数据库存储照片

新建表img,设置id字段和imgs字段(设置为blob属性,二进制数据)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2017/8/18 11:37
  4. # @File : Picture.py
  5. import MySQLdb as mysql
  6. import sys
  7. conn=mysql.connect(host= 'localhost',user= 'root',passwd= 'xxx',db= 'mydata')
  8. fp = open( "./1.jpg")
  9. img = fp.read()
  10. fp.close()
  11. #存入图片
  12. def insert_imgs(img):
  13. # mysql连接
  14. cursor = conn.cursor()
  15. # 注意使用Binary()函数来指定存储的是二进制
  16. # cursor.execute("insert into img set imgs='%s'" % mysql.Binary(img))
  17. cursor.execute( "Insert into img(imgs) values(%s)", (mysql.Binary(img)))
  18. # 如果数据库没有设置自动提交,这里要提交一下
  19. conn.commit()
  20. cursor.close()
  21. # 关闭数据库连接
  22. conn.close()
  23. #提取图片
  24. def select_imgs(img):
  25. cursor=conn.cursor()
  26. cursor.execute( 'select imgs from img')
  27. print cursor.fetchall()
  28. cursor.close()
  29. conn.close()
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值