psycopg2 does not insert unicode data

假装没事ソ 提交于 2019-12-11 04:37:40

问题


I have a script that takes data from one database and according to tables' names and fields copies it into another database. The issue is about unicode data, I need to insert some words in Russian but every time psycopg2 writes it as if it were default string.

import psycopg2
import psycopg2.extensions

conn_two = psycopg2.connect(user="postgres", password="password", host = "localhost", port= "5432", dbname = "base2")
cur_2  = conn_two.cursor()
sql = 'INSERT INTO {} ({})  VALUES {};'.format('"tb_names"', '"num", "name", "district"',  (23, 'Рынок', 'Волжский')) 
cur_2.execute(sql)
conn_two.commit()

Here is how the result looks like in pgAdmin4:

I also tried to set exteinsions and insert data in unicode, but in this case I have an error

import psycopg2
import psycopg2.extensions
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)

conn_two = psycopg2.connect(user="postgres", password="password", host = "localhost", port= "5432", dbname = "base2")
conn_two.set_client_encoding("utf-8")
conn_two.set_client_encoding('UNICODE')
cur_2  = conn_two.cursor()
sql = 'INSERT INTO {} ({})  VALUES {};'.format('"tb_names"', '"num", "name", "district"',  (23, u'Рынок', u'Волжский')) 
cur_2.execute(sql)
conn_two.commit()


Traceback (most recent call last):
  File "D:\_Scripts\pgadmin.py", line <>, in <module>
    cur_2.execute(sql)
psycopg2.ProgrammingError: ОШИБКА:  тип "u" не существует # - says that type "u" does not exist
LINE 1: ...ing_ex" ("num", "name", "district")  VALUES (23, u'\u0420\u...
                                                            ^

What should be done here?


回答1:


Don't prepare your string with the values baked in (using string formatting or concatenation).

Instead, pass your values to the .execute method as an argument.

E.g.

sql = 'INSERT INTO {} ({})  VALUES (%s, %s, %s);'.format('"tb_names"', '"num", "name", "district"') 
cur_2.execute(sql, (23, u'Рынок', u'Волжский'))


来源:https://stackoverflow.com/questions/53375955/psycopg2-does-not-insert-unicode-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!