我正在尝试在blob数据类型中打印内容,这是我的python代码以及错误。
通过python读取blob数据的最佳方法是什么?
真的很感谢这里的任何帮助。
链接到表格屏幕截图。
我知道blob数据类型列包含浮点数,下面给出示例。
6.213203675953e-311 1.69759663307e-313 3e-323 1.697596633e-313 2.121995969e-314 5e-324 6.1113478779844e-311 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 4.473620042652186 。 。 。
import mysql.connector as mariadb
db_config = {
'host': '123.0.0.0',
'port': '1000',
'database': 'any_db',
'user': 'user_name',
'password': 'pass_word'
}
def read_blob(cell, experiment):
sql = """select Tuning_Curve_AuC from Cell_Table where cell = %s and experiment = %s"""
try:
# query blob data form the authors table
conn = mariadb.connect(**db_config)
cursor = conn.cursor()
cursor.execute(sql, (cell, experiment))
blob = cursor.fetchone()[0]
print(blob)
except(Exception, mariadb.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')
def main():
read_blob(1, 'E40_062716')
if __name__ == '__main__':
main()
错误:
<built-in method fetch_row of _mysql_connector.MySQL object at 0x7ff57f2b5e00> returned a result with an error set
数据库连接已关闭。
答案 0 :(得分:0)
我做了一些挖掘并找到了解决方案。事实证明,我们必须获取原始数据并使用数组将其转换为小数(浮动)。这是代码片段的一部分。
conn = mariadb.connect(**db_config)
cursor = conn.cursor(raw=True)
cursor.execute(sql, (cell, experiment))
blob = cursor.fetchone()[0]
doubles_sequence = array.array('d', blob)
print(doubles_sequence)
当然,您必须“导入数组”。