Unable to convert PostgreSQL text column to bytea

后端 未结 1 2013
执念已碎
执念已碎 2020-12-11 05:12

In my application I am using a postgresql database table with a \"text\" column to store pickled python objects. As database driver I\'m using psycopg2 and until now I only

1条回答
  •  一整个雨季
    2020-12-11 05:19

    The problem is that casting text to bytea doesn't mean, take the bytes in the string and assemble them as a bytea value, but instead take the string and interpret it as an escaped input value to the bytea type. So that won't work, mainly because pickle data contains lots of backslashes, which bytea interprets specially.

    Try this instead:

    SELECT convert_to(data, 'LATIN1') ...
    

    This converts the string into a byte sequence (bytea value) in the LATIN1 encoding. For you, the exact encoding doesn't matter, because it's all ASCII (but there is no ASCII encoding).

    0 讨论(0)
提交回复
热议问题