Recently encountered a problem with Python accessing SQL Server, summarized here.
One, configure Python to access 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. Accessing SQL Server
Copy the 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")
II. Configure Python to access SQL Server under Linux
Environment: CentOS 6.2 + SQL Server 2008
1, installation FreeTDS:
Copy the Code code as follows:
Yum Install freetds*
2, installation Pyodbc:
Copy CodeThe code is as follows:
Yum Install Pyodbc
To modify the ODBC configuration:
Copy the Code code as follows:
Vi/etc/odbcinst.ini
Add FreeTDS driver:
Copy the 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 the 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")