2017-02-16 96 views
1

我需要创建一个python脚本来将数据从Microsoft SQL Server迁移到PostgreSql。该问题发生在非ascii字符。在Microsoft SQL Server我有一个类型为nvarchar的列用来存储从ms sql到postgres nonascii字符的迁移错误python

â 

我使用pyodbc检索字符一个字符一个名为table1表。我的连接字符串是

"ms_sql":{ 
    "DRIVER":"{SQL Server Native Client 11.0}", 
    "SERVER":"(localdb)\\v11.0", 
    "DATABASE":"sheshant_database", 
    "Trusted_Connection":"yes", 
    "charset":"SQL_Latin1_General_CP1_CI_AS" 

ms sql中的排序规则是SQL_Latin1_General_CP1_CI_AS。 当我检索的Python数据,它给

u'\xe2' 

然后我通过psycopg2连接Postgres的,这是我的连接字符串参数

"postgres":{ 
    "host":"127.0.0.1", 
    "user":"postgres", 
    "password":"regards", 
    "database":"cmots", 
    "port":"5432" 

在PostgreSQL,客户端和服务器的编码是“latin1的”和'utf8'。我使用psycopg2中的命令

'insert into table1 values ('+ a +');' // here a is a unicode and a = u'\xe2' 

。但是存储在PostgreSQL中的数据是

Γ 

哪里出错了?

+0

如果'pyodbc.version'不返回 '4.0.6' 然后更新pyodbc到最新版本。最近有几个变化可能会影响你的结果。 –

+0

另外,试试'cursor.execute('insert into table1 values(%s)',(a,))' –

回答

0

检查Windows编码并替换:

cursor.execute ('insert into table1 values (%s)', a.decode('latin'));