Introduction to using Python to access SqlServer in Windows and Linux, pythonsqlserver

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.