pyodbc连接SQL Server出现中文乱码问题的解决方案

添加一个配置参数unicode_results=True

pyodbc.connect(connStr,unicode_results=True)

资料(摘自http://code.google.com/p/pyodbc/wiki/Module):

connect

connect(*connectionstring, **kwargs) --> Connection

Creates and returns a new connection to the database.

The optional connection string can be passed as a string or Unicode object (connectionstring), as keywords (kwargs), or both.

# a string
cnxn = connect('driver={SQL Server};server=localhost;database=test;uid=me;pwd=me2')
# keywords
cnxn = connect(driver='{SQL Server}', server='localhost', database='test', uid='me', pwd='me2')
# both
cnxn = connect('driver={SQL Server};server=localhost;database=test', uid='me', pwd='me2')

The DB API recommends some keywords that are not usually used in ODBC connection strings, so they are converted:

fromconverted to
hostserver
useruid
passwordpwd

Some keywords are used by pyodbc and are not passed to the odbc driver:

keyworddescriptiondefault
autocommitIf False, Connection.commit must be called; otherwise each statement is automatically commitedFalse
ansiIf True, the driver does not support Unicode and SQLDriverConnectA should be usedFalse
unicode_resultsIf True, strings returned in result sets are always Unicode (2.1.5+)False
readonlyIf True, the connection is set to readonlyFalse

The ansi keyword should only be used to work around driver bugs. pyodbc will determine if the Unicode connection function (SQLDriverConnectW) exists and always attempt to call it. If the driver returns IM001 indicating it does not support the Unicode version, the ANSI version is tried (SQLDriverConnectA). Any other SQLSTATE is turned into an exception. Setting ansi to true skips the Unicode attempt and only connects using the ANSI version. This is useful for drivers that return the wrong SQLSTATE (or if pyodbc is out of date and should support other SQLSTATEs).

For help on connection strings, see ConnectionStrings


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值