【问题标题】:how to retrieve file stored as BLOB in mysqlDB using python如何使用python检索在mysqlDB中存储为BLOB的文件
【发布时间】:2019-05-10 12:26:51
【问题描述】:

我的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_blobdef 和update_blobdef 工作正常,但我在运行read_blobdef 时出现此错误:

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

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

【问题讨论】:

  • 你认为out=cursor.fetchall() 会做什么?

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 2021-12-23
    • 1970-01-01
    • 2021-01-31
    • 2012-10-13
    • 2012-04-13
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    相关资源
    最近更新 更多