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

标签 python python-3.x blob mysql-python

我的表(作者)是:

--------------
| 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 值。

最佳答案

通过替换此代码,问题解决了,但我不知道为什么!!!!

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()

关于python - 如何使用 python 检索 mysqlDB 中存储为 BLOB 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53691530/

相关文章:

python - 根据条件延迟拆分 Iterable

python - 我怎样才能找到所有 ydl_opts

python - 如何通过代码注册python3实例方法进行查找?

python - 使用 TravisCI 在 Anaconda 上部署 Python 包

python - 如何对 Pandas Dataframe 的 YAML 进行非规范化?

python - DBSCAN的参数eps,python

python-3.x - 如何让 discord bot 队列本地 mp3?

mysql - 在 2017 年将图像存储为 blob?

javascript - 使用 CKEditor、BLOB 字段和 Rails 4 自定义上传图像

sql - 甲骨文10 : Using HEXTORAW to fill in blob data