This article describes how to use Python to access SqlServer in Windows and Linux. This article describes how to configure Python to access Sqlserver in Windows and configure Python to access SqlServer in Linux, if you need it, you can refer to and often use Python to write a demo to verify the feasibility of the solution. recently, you have encountered the problem of accessing SqlServer through Python. here is a summary.
1. configure Python to access Sqlserver in Windows
Environment: Windows 7 + Sqlserver 2008
1. download and install pyodbc
: Http://code.google.com/p/pyodbc/downloads/list
2. access SqlServer
The code is 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.exe cute ("select * from Tb ")
II. configure Python to access SqlServer in Linux
Environment: CentOS 6.2 + Sqlserver 2008
1. install freetds:
The code is as follows:
Yum install freetds *
2. install pyodbc:
The code is as follows:
Yum install pyodbc
Modify odbc configuration:
The code is as follows:
Vi/etc/odbcinst. ini
Add FreeTDS driver:
The code is as follows:
[SQL Server]
Description = FreeTDS ODBC driver for MSSQL
Driver =/usr/lib/libtdsodbc. so
Setup =/usr/lib/libtdsS. so
FileUsage = 1
3. test
The code is 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.exe cute ("select * from Tb ")
Here, I just wrote a simple demo to verify the feasibility. I hope it will help you.