Database 如何使用python在IBMDB2数据库中存储映像?
我可以使用python文件连接到ibmdb2数据库,我可以通过它运行各种命令,但是如何将图像存储到DB2数据库表中的列中呢?我已经在我的表中创建了一个BLOB类型的列。为什么不看看github上ibm_db的测试用例呢 它们显示了一个测试用例,用于使用Database 如何使用python在IBMDB2数据库中存储映像?,database,db2,blob,azure-blob-storage,Database,Db2,Blob,Azure Blob Storage,我可以使用python文件连接到ibmdb2数据库,我可以通过它运行各种命令,但是如何将图像存储到DB2数据库表中的列中呢?我已经在我的表中创建了一个BLOB类型的列。为什么不看看github上ibm_db的测试用例呢 它们显示了一个测试用例,用于使用PARAM_FILE选项ibm_db.bind_PARAM()将图像文件的内容复制到BLOB列中 请参见testcase[此处][1] 尽管测试可能已过期,但下面的代码片段 显示通过PARAM_file方法使用ibm_db 3.0.1版(成功地将j
PARAM_FILE
选项ibm_db.bind_PARAM()
将图像文件的内容复制到BLOB
列中
请参见testcase[此处][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))