Introduction to using Python to access SqlServer in Windows and Linux, pythonsqlserver
I often use Python to write a demo to verify the feasibility of the solution. I recently 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
Copy codeThe 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:
Copy codeThe Code is as follows:
Yum install freetds *
2. Install pyodbc:
Copy codeThe Code is as follows:
Yum install pyodbc
Modify odbc Configuration:
Copy codeThe Code is as follows:
Vi/etc/odbcinst. ini
Add FreeTDS DRIVER:
Copy codeThe 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
Copy codeThe 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.