2017-02-16 94 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'));