虚位以待(AD)
虚位以待(AD)
首页 > 脚本专栏 > python > python访问sqlserver示例

python访问sqlserver示例
类别:python   作者:码皇   来源:互联网   点击:

这篇文章主要介绍了python访问sqlserver示例,需要的朋友可以参考下

最近遇到了Python访问SqlServer的问题,这里总结下。

一、Windows下配置Python访问Sqlserver

环境:Windows 7 + Sqlserver 2008

1、下载并安装pyodbc

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

2、访问SqlServer

复制代码 代码如下:

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

 

二、Linux下配置Python访问SqlServer

环境:CentOS 6.2 + Sqlserver 2008

1、安装freetds:

复制代码 代码如下:

yum install freetds*


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

相关热词搜索: sqlserver