SQL server access using python

Source: Internet
Author: User
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 ")

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.