Introduction to accessing SQL Server using Python under Windows and Linux

Source: Internet
Author: User
Tags odbc
Often use Python to write the demo to verify the feasibility of the scheme, recently encountered a Python access to SQL Server problem, summarized here.

One, configure Python to access SQL Server under Windows

Environment: Windows 7 + SQL Server 2008

1. Download and install Pyodbc

Download Address: Http://code.google.com/p/pyodbc/downloads/list

2. Accessing SQL Server

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.execute ("SELECT * from Tb")


II. Configure Python to access SQL Server under Linux

Environment: CentOS 6.2 + SQL Server 2008

1, installation FreeTDS:

The code is as follows:


Yum Install freetds*

2, installation Pyodbc:

The code is as follows:


Yum Install Pyodbc


To modify the 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. Testing

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.execute ("SELECT * from Tb")

Here is just a simple demo to verify the feasibility, hope to help you.

  • 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.