【问题标题】:Migration from ms sql to postgres nonascii characters error python从 ms sql 迁移到 postgres nonascii 字符错误 python
【发布时间】:2017-02-16 18:41:21
【问题描述】:

我需要创建一个 python 脚本来将数据从 Microsoft SQL Server 迁移到 PostgreSql。非 ascii 字符会出现问题。在 Microsoft SQL Server 中,我有一个名为 table1 的表,其中有一列 nvarchar 类型的列存储一个字符

â

我正在使用 pyodbc 来检索字符。我的连接字符串是

"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 中存储的数据是

Γ

哪里出错了?

【问题讨论】:

  • 如果 pyodbc.version 没有返回 '4.0.6' 则将 pyodbc 更新到最新版本。最近有几项更改可能会影响您的结果。
  • 另外,试试cursor.execute('insert into table1 values (%s)', (a, ))

标签: python sql-server postgresql psycopg2 pyodbc


【解决方案1】:

检查 Windows 编码并替换它:

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

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 2011-09-04
    • 2021-05-06
    • 2012-02-14
    相关资源
    最近更新 更多