您的位置:首页 > 数据库

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

2013-05-17 16:44 881 查看
添加一个配置参数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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: