用Python訪問SqlServer

常常用Pythondemo來驗證方案的可行性,最近遇到了Python訪問SqlServer的問題,這裏總結下。python

1、Windows下配置Python訪問Sqlserversql

環境:Windows 7 + Sqlserver 2008測試

1、下載並安裝pyodbcgoogle

下載地址:http://code.google.com/p/pyodbc/downloads/list spa

2、訪問SqlServercode

>>> 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")

2、Linux下配置Python訪問SqlServerserver

環境:CentOS 6.2 + Sqlserver 2008ip

1、安裝freetdsci

yum install freetds*get

2、安裝pyodbc

yum install pyodbc

修改odbc配置:

    vi /etc/odbcinst.ini

添加FreeTDS驅動:

[SQL Server]

Description = FreeTDS ODBC driver for MSSQL

Driver = /usr/lib/libtdsodbc.so

Setup = /usr/lib/libtdsS.so

FileUsage = 1

3、測試

#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")
相關文章
相關標籤/搜索