This article mainly introduces the example of accessing sqlserver through python. For more information about how to access SqlServer through Python, see the following.
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 ")