zoukankan      html  css  js  c++  java
  • MySQL简单的存储图片信息

    MySQL存储图片的二进制,其字段设置为blob属性,二进制数据

    1、连接数据库

    1 import pymysql
    2 import sys
    3  
    4 conn=pymysql.connect(host='localhost',user='root',passwd='xxx',db='mydata')

    2、打开存储图片路径

    1 fp = open("./1.jpg")
    2 img = fp.read()
    3 fp.close()

    3、存储图片

     1 def insert_imgs(img):
     2     # mysql连接
     3  
     4     cursor = conn.cursor()
     5     # 注意使用Binary()函数来指定存储的是二进制
     6     # cursor.execute("insert into img set imgs='%s'" % mysql.Binary(img))
     7     cursor.execute("Insert into img(imgs) values(%s)", (mysql.Binary(img)))
     8     # 如果数据库没有设置自动提交,这里要提交一下
     9     conn.commit()
    10     cursor.close()
    11     # 关闭数据库连接
    12     conn.close()

    4、提取图片

    1  def select_imgs(img):
    2     cursor=conn.cursor()
    3     cursor.execute('select imgs from img')
    4     print cursor.fetchall()
    5     cursor.close()
    6     conn.close()

    版权声明:本文为CSDN博主「Coder_py」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/Coder_py/article/details/77368034

  • 相关阅读:
    HZOJ 通讯
    HZOJ 礼物
    HZOI 可怜与超市
    高二小假期集训—D5
    [BZOJ3566][SHOI2014]概率充电器
    [***]HZOI20190714 T2熟练剖分
    20190714(又一次翻车……)
    HZOI20190714 T3建造游乐场
    模板—慢速乘
    模板—十进制快速幂
  • 原文地址:https://www.cnblogs.com/liaopeng123/p/11381140.html
Copyright © 2011-2022 走看看