繁体   English   中英

如何使用python检索在mysqlDB中存储为BLOB的文件

[英]how to retrieve file stored as BLOB in mysqlDB using python

我的table (authors)是:

--------------
| id | photo |
--------------
| 14 | BLOB  |
--------------

照片列类型为BLOB

我想使用python检索此Blob值:

我的代码如下:

import mysql.connector

db_username='mehdi'
db_password='mehdi'
database_name='cra_db'
db_host='127.0.0.1'

def read_file(filename):
    with open(filename, 'rb') as f:
        photo = f.read()
    return photo

def write_file(data, filename):
    with open(filename, 'wb') as f:
        f.write(data)

def write_blob(author_id, filename):
    # read file
    data = read_file(filename)
    # prepare update query and data
    query = "INSERT INTO `cra_db`.`authors` (`id`,`photo`) VALUES(%s,%s)"
    args = (author_id,data)
    try:
        cnx = mysql.connector.connect(user=db_username, password=db_password, host=db_host, database=database_name)
        cursor = cnx.cursor()
        cursor.execute(query, args)
        cnx.commit()
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        cnx.close()

def update_blob(author_id, filename):
    # read file
    data = read_file(filename)
    # prepare update query and data
    query = "UPDATE authors " \
            "SET photo = %s " \
            "WHERE id  = %s"
    args = (data, author_id) 
    try:
        cnx = mysql.connector.connect(user=db_username, password=db_password, host=db_host, database=database_name)
        cursor = cnx.cursor()
        cursor.execute(query, args)
        cnx.commit()
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        cnx.close()

def read_blob(author_id, filename):
    # select photo column of a specific author
    query = "SELECT photo FROM authors WHERE id = {}".format(author_id)
    try:
        cnx = mysql.connector.connect(user=db_username, password=db_password, host=db_host, database=database_name)
        cursor = cnx.cursor()
        cursor.execute(query)
        out=cursor.fetchall()
        # write blob data into a file
        write_file(out, filename)
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        cnx.close()

def main():
    write_blob(14,"01.jpg")
    update_blob(14, "01.jpg")
    read_blob(14,"02.jpg")

if __name__ == '__main__':
    main()

write_blob def和update_blob def正常工作,但是我在运行read_blob def时发生此错误:

 <built-in method fetch_row of _mysql_connector.MySQL object at 0x00000000034DA8E0> returned a result with an error set 

您有解决此问题的想法吗? 我想检索此Blob值。

通过替换此代码,问题得以解决,但idont知道原因!!!!

from mysql.connector import MySQLConnection, Error,connect

db_username='mehdi'
db_password='mehdi'
database_name='cra_db'
db_host='127.0.0.1'

def read_file(filename):
    with open(filename, 'rb') as f:
        photo = f.read()
    return photo

def write_file(data, filename):
    with open(filename, 'wb') as f:
        f.write(data)

def write_blob(author_id, filename):
    # read file
    data = read_file(filename)
    # prepare update query and data
    query = "INSERT INTO `cra_db`.`authors` (`id`,`photo`) VALUES(%s,%s)"
    args = (author_id,data)
    try:
        cnx = MySQLConnection(user=db_username, password=db_password, host=db_host, database=database_name)
        cursor = cnx.cursor()
        cursor.execute(query, args)
        cnx.commit()
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        cnx.close()

def update_blob(author_id, filename):
    # read file
    data = read_file(filename)
    # prepare update query and data
    query = "UPDATE authors " \
            "SET photo = %s " \
            "WHERE id  = %s"
    args = (data, author_id) 
    try:
        cnx = MySQLConnection(user=db_username, password=db_password, host=db_host, database=database_name)
        cursor = cnx.cursor()
        cursor.execute(query, args)
        cnx.commit()
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        cnx.close()

def read_blob(author_id, filename):
    # select photo column of a specific author
    query = "SELECT photo FROM authors WHERE id = %s"
    try:
        cnx = MySQLConnection(user=db_username, password=db_password, host=db_host, database=database_name)
        cursor = cnx.cursor()
        cursor.execute(query, (author_id,))
        photo=cursor.fetchone()[0]
        # write blob data into a file
        write_file(photo, filename)
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        cnx.close()

def main():
    # write_blob(144,"01.jpg")
    # update_blob(144, "01.jpg")
    read_blob(144,"02.jpg")

if __name__ == '__main__':
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM