随笔 - 272  文章 - 0  评论 - 283  阅读 - 142万

用Python访问SqlServer

经常用Pythondemo来验证方案的可行性,最近遇到了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")

 

这里只是写了简单的demo来验证可行性,希望对你有帮助。

posted on   Mike_Zhang  阅读(14306)  评论(5编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2012年4月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

点击右上角即可分享
微信分享提示