将加载与从SQL表读取的数据一起使用时,Python Pickle提供EOFError

时间:2018-10-12 05:40:23

标签: python pickle

我正在尝试加载BLOB保存的字段-使用以下代码从mysql读取后进行嵌入:

try:
    print("executing query >> " + str(sql))
    cursor.execute(sql)

    for tag, embedding in cursor.fetchall():
        result['TAG'] = tag
        result['EMBEDDING'] = pickle.loads(embedding)

except Exception as e:
    print(e)
    traceback.print_exc()

错误:

Traceback (most recent call last):
  File "/home/xxx/sql_test.py", line 44, in get_existing_embeddings
  result['EMBEDDING'] = pickle.loads(emb)
  EOFError: Ran out of input
  File "/home/xxx/SETUP/pycharm-community-2018.2.1/helpers/pydev/pydevd.py", line 1664, in <module>
  main()

无法输入

但是,在调试时,我看到嵌入变量不是空的:附加的快照:

enter image description here

保存BLOB字段的代码:

data["EMBEDDING_DATA"]=numpy.array(image_embedding).dumps()
..
.
sql_stmt="INSERT INTO "+DatabaseCredentials.DATABASE+"."+DatabaseCredentials.EMBEDDING_TABLE_NAME
sql_stmt = sql_stmt + " (TAG ,DOMAIN,PICTURE_ID,IMAGE_PATH,EMBEDDING_DATA,EMBEDDING_BASE_RESOLUTION,CREATED_AT,STATUS) "
sql_stmt = sql_stmt + " VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
args = (data["TAG"],
        data["DOMAIN"],
        data["PICTURE_ID"],
        data["URL"],
        data["EMBEDDING_DATA"],
        data["EMBEDDING_DATA_RESOLUTION"],
        now,
        data["STATUS"]
        )
try:
    cursor.execute(sql_stmt,args)
    res = db.commit()
except connector.errors as e:
    print(e)

我在查询sql表时也可以看到此数据。我想念什么?

1 个答案:

答案 0 :(得分:0)

原因是sql表中的blob字段不足以供我尝试保存的pickle转储。我将sql表中的列更改为longblob,现在它可以正常工作。