Home > Backend Development > Python Tutorial > python访问sqlserver示例

python访问sqlserver示例

WBOY
Release: 2016-06-16 08:45:19
Original
1448 people have browsed it

最近遇到了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")
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template