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






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:

from converted to
host server
user uid
password pwd

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

keyword description default
autocommit If False, Connection.commit must be called; otherwise each statement is automatically commited False
ansi If True, the driver does not support Unicode and SQLDriverConnectA should be used False
unicode_results If True, strings returned in result sets are always Unicode (2.1.5+) False
readonly If True, the connection is set to readonly False

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