Postgres mogrify将二进制文件添加到SQL字符串

时间:2017-07-15 00:43:21

标签: python postgresql binary

我正在尝试使用python项目dedupe中的示例保存数据。当我尝试将数据插回到数据库中时,我得到的错误即将结束。

我得到的错误是:

Traceback (most recent call last):
  File "dedupe_orgs.py", line 11, in <module>
    main()
  File "dedupe_orgs.py", line 7, in main
    entity.dedupe_orgs()
  File "/orgs.py", line 183, in dedupe_orgs
    c2.execute("INSERT INTO entity_organization %s VALUES %s" % (values, args_str))
psycopg2.ProgrammingError: syntax error at or near "b"
LINE 1: ...d,id,name,created_on,updated_on,entity_id) VALUES b"(0,65,'S...

这是相关代码:

num_cols = len(column_names)
    mog = "(" + ("%s," * (num_cols - 1)) + "%s)"
    args_str = ','.join(c2.mogrify(mog, x) for x in full_data)
    values = "(" + ','.join(x for x in column_names) + ")"
    c2.execute("INSERT INTO entity_organization %s VALUES %s" % (values, args_str))
    con2.commit()
    con2.close()
    con.close()

我认为问题是该示例是用Python 2编写的,但我使用的是Python 3.如何解决错误,以便不会将'b'添加到每个查询中,从而阻止数据保存?

1 个答案:

答案 0 :(得分:0)

我猜测args_str实际上并不是类型字符串。您使用的是pycharm之类的正确调试器吗?如果是这样,你可以在尝试插入之前检查变量以确认它是一个字符串吗?