Linux python accesses SQL Server

Source: Internet
Author: User

Operation with Root privileges


1. Install Unixodbc "http://www.unixodbc.org/"

SH Configure--PREFIX=/USR/LOCAL/UNIXODBC

Make

Make install


2. Install FreeTDS "http://www.freetds.org/software.html"

SH Configure-prefix=/usr/local/freetds--with-unixodbc=/usr/local/unixodbc/

-with-tdsver=8.0-enable-msdblib-enable-dbmfix-with-gnu-ld-enable-shared-enable-static

Make

Make install


Note: Indicate that--WITH-UNIXODBC=/USR/LOCAL/UNIXODBC can generate libtdsodbc.so


3. Configure UNIXODBC


Enter/USR/LOCAL/UNIXODBC

Create File Tds.driver.template


[TDS]

Description = FreeTDS Driver for Linux & MSSQL on Win32

Driver =/usr/local/freetds/lib/libtdsodbc.so

Setup =/usr/local/freetds/lib/libtdss.so

Execute commands after saving


Bin/odbcinst-i-d-f Tds.driver.template


Create File Tds.datasource.template


[My_data]

Description = Connection to Windows virtual machine

Driver = TDS

Trace = No

Database = AA

Server = 192.168.7.3

Port = 1433

Tds_version = 8.0

Execute commands after saving


Bin/odbcinst-i-s-l-F tds.datasource.template


4. Test FreeTDS and UNIXODBC

Enter the/usr/local/freetds/directory

Execute command

bin/tsql-h192.168.7.3-p1433-usa-padmin1234

1>use AA

2>select * FROM Sysfiles

3>go

Re-entry

/usr/local/unixodbc

Execute command

Bin/isql my_data SA admin1234

Sql>select * FROM Sysfiles


5. Install Pyodbc "http://code.google.com/p/pyodbc/"

First install version python2.4.4, step:

./configure

Make

Make install

Then compile the installation

Python setup.py Install

Note: If the compilation error occurs, please

cp/usr/local/unixodbc/include/*/usr/include/

cp/usr/local/unixodbc/lib/*/usr/lib/

And then run


6.python Test Code

Import Pyodbc

conn = Pyodbc.connect ("Dsn=my_data; Uid=sa; PWD=ADMIN1234;DATABASE=AA; ")

sql = ' SELECT * from Sysfiles '

cursor = Conn.cursor ()

Cursor.execute (SQL)

rs = Cursor.fetchall ()

If Len (RS) > 0:

For I in range (0, Len (RS)):

row = Rs[i]

Print Row[0]

Cursor.close ()

Conn.close ()


*7. If PYODBC is used under other accounts

Copy the 5th step generated file pyodbc.so to the Zenoss python directory

cp/usr/local/lib/python2.4/site-packages/pyodbc.so/opt/zenoss/lib/python2.4/site-packages/

"Pyodbc.so may be located in the/usr/lib/python2.4/site-packages/directory"

This allows Zenoss users to use PYODBC.


This article is from the "7727197" blog, please be sure to keep this source http://7737197.blog.51cto.com/7727197/1663087

Linux python accesses SQL Server

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.