Recently encountered a problem with Python access to SQL Server, summarized here.
A. Configure Python access to SQL Server under Windows
Environment: Windows 7 + SQL Server 2008
1. Download and install Pyodbc
Download Address: Http://code.google.com/p/pyodbc/downloads/list
2. Access SQL Server
Copy Code code as follows:
>>> Import Pyodbc
>>>CNXN = Pyodbc.connect (' Driver={sql Server}; Server=192.168.1.100\\sql;database=testdb; Uid=sa; Pwd=mypassword ')
>>>cursor = Cnxn.cursor ()
>>>cursor.execute ("SELECT * from Tb")
Second, Linux configuration python access to SQL Server
Environment: CentOS 6.2 + SQL Server 2008
1, installation FreeTDS:
Copy Code code as follows:
2, installation Pyodbc:
Copy Code code as follows:
To modify an ODBC configuration:
Copy Code code as follows:
Add FreeTDS driver:
Copy Code code as follows:
[SQL Server]
Description = FreeTDS ODBC driver for MSSQL
Driver =/usr/lib/libtdsodbc.so
Setup =/usr/lib/libtdss.so
FileUsage = 1
3, testing
Copy Code code as follows:
#python
>>> Import Pyodbc
>>>CNXN = Pyodbc.connect (' Driver={sql Server}; Server=192.168.1.100\\sql;database=testdb; Uid=sa; Pwd=mypassword ')
>>>cursor = Cnxn.cursor ()
>>>cursor.execute ("SELECT * from Tb")