如何使用 python 在 IBM DB2 数据库中存储图像?

How to store an image in an IBM DB2 Database using python?

我可以使用 python 文件连接到我的 IBM DB2 数据库,我可以通过它 运行 各种命令,但是我如何将图像存储到我的 table 在 DB2 数据库上?我在 table.

中创建了一个 BLOB 类型的列

为什么不在 github 上查看 ibm_db 的测试用例?

他们展示了一个测试用例,使用 PARAM_FILE 选项 ibm_db.bind_param() 将图像文件的内容复制到 BLOB 列中。

在[此处][1]查看测试用例。

尽管测试可能已过时,但以下代码片段 显示通过 PARAM_FILE 方法使用 ibm_db 版本 3.0.1(成功将 jpg 文件插入 BLOB 列)工作的参数:

jpg_file="/home/some_user/Pictures/houston.jpg"  # path to the image file

# table my_pics already exists with a blob colum called jpg_content of appropriate length
# The database already contains a table called MY_PICS in the current schema
# with a BLOB column named JPG_CONTENT
#

insert_sql = "INSERT INTO my_pics(jpg_content) values(?)"
try:
    stmt = ibm_db.prepare(conn, insert_sql)
    print("Successfully prepared the insert statement")
except:
    print("Failed to compile the insert statement")
    print(ibm_db.stmt_errormsg(stmt))
    ibm_db.close(conn)
    sys.exit(1)

# link a file-name to the parameter-marker of the insert-statement (target column is BLOB)

try:
    rc = ibm_db.bind_param(stmt, 1, jpg_file, ibm_db.PARAM_FILE,ibm_db.SQL_BLOB )
    print("Bind returned: "+str(rc))
    print("Successfully bound the filename to the parmameter-marker")

except:
    print("Bind returned: "+str(rc))
    print("Failed to bind the input parameter file")
    print(ibm_db.stmt_errormsg(stmt))
    ibm_db.close(conn)
    sys.exit(1)

try:
    ibm_db.execute(stmt)
    print("Successfully inserted jpg file into blob column")
except:
    print("Failed to execute the insert to blob column")
    print(ibm_db.stmt_errormsg(stmt))