windows - Postgresql and Python: Insert UTF-8 string -
i trying insert list of strings postgres table.
the strings folder names, gathered windows machine, rewritten unix-style string.
this works, when there foldername "üöä" insert not fail, inserts empty string.
here code use insert values:
def db_insert_paths(paths): paths.sort() path in paths: print(path) print(type(path)) cur.execute("insert rasp (folder) values (%s)", (path,)) cur.close() conn.close() return 0
here examples: captured traffic sent postgres server.
/mnt/hdd/bilder/2004/2004.08.15. dorffest <class 'str'> insert rasp (folder) values ('/mnt/hdd/bilder/2004/2004.08.15. dorffest')
this insert works. data in table.
however, next 1 doesn't:
/mnt/hdd/bilder/2004/2004.08.30. filterschacht räumen und reinigen <class 'str'> insert rasp (folder) values ('/mnt/hdd/bilder/2004/2004.08.30. filterschacht r��umen und reinigen')
the entry empty.
what doing wrong here? guessing kind of encoding fail.
i using python3.4 on windows 8.1, postgresql 9.5. field type "text".
i believe want encode string here.
cur.execute("insert rasp (folder) values (%s)", (path.encode('utf-8',))
and when want read db you'll decode path.decode('utf-8')
alternatively, change db character set: https://www.postgresql.org/docs/current/static/multibyte.html#aen32089
Comments
Post a Comment