需求:
在linux用python连接sqlserver2008对数据库进行查询检测等操作
环境:
CentOS release 5.3 (Final) 2.6.18-128.el5 x86_64 GNU/Linux
Python 2.4.3
Window Server 2008 Standard
Sqlserver 2008
linux配置python连接sqlserver:
1.安装freetds:
yum install freetds*
2.安装pyodbc
下载2.1.7版本,解压pyodbc-2.1.7.zip后修改setup.py
查找出一下两行代码:
- extra_compile_args = None
- extra_link_args = None
修改为:
- extra_compile_args = ['-Wno-write-strings', '-I/usr/include']
- extra_link_args = ['-L/usr/lib']
这两个的修改主要是根据自身linux系统安装好的unixodbc包的环境进行相应配置:
- # rpm -ql unixODBC-devel-2.2.11-10.el5.x86_64
- /usr/include/autotest.h
- /usr/include/odbcinst.h
- /usr/include/odbcinstext.h
- /usr/include/sql.h
- /usr/include/sqlext.h
- /usr/include/sqltypes.h
- /usr/include/sqlucode.h
- /usr/include/uodbc_stats.h
- /usr/lib64/libboundparam.a
- /usr/lib64/libboundparam.so
- /usr/lib64/libesoobS.a
- /usr/lib64/libesoobS.so
- /usr/lib64/libgtrtst.a
- /usr/lib64/libgtrtst.so
- /usr/lib64/libmimerS.a
- /usr/lib64/libmimerS.so
- /usr/lib64/libnn.a
- /usr/lib64/libnn.so
- /usr/lib64/libodbc.a
- /usr/lib64/libodbccr.a
- /usr/lib64/libodbccr.so
- /usr/lib64/libodbcdrvcfg1S.a
- /usr/lib64/libodbcdrvcfg1S.so
- /usr/lib64/libodbcdrvcfg2S.a
- /usr/lib64/libodbcdrvcfg2S.so
- /usr/lib64/libodbcinst.a
- /usr/lib64/libodbcinst.so
- /usr/lib64/libodbcinstQ.a
- /usr/lib64/libodbcminiS.a
- /usr/lib64/libodbcminiS.so
- /usr/lib64/libodbcmyS.a
- /usr/lib64/libodbcnnS.a
- /usr/lib64/libodbcnnS.so
- /usr/lib64/libodbcpsql.a
- /usr/lib64/libodbcpsqlS.a
- /usr/lib64/libodbctxtS.a
- /usr/lib64/libodbctxtS.so
- /usr/lib64/liboplodbcS.a
- /usr/lib64/liboplodbcS.so
- /usr/lib64/liboraodbcS.a
- /usr/lib64/liboraodbcS.so
- /usr/lib64/libsapdbS.a
- /usr/lib64/libsapdbS.so
- /usr/lib64/libtdsS.a
- /usr/lib64/libtdsS.so
- /usr/lib64/libtemplate.a
- /usr/lib64/libtemplate.so
3.配置odbc
- # more /etc/odbcinst.ini
- # Example driver definitions
- # See /usr/share/doc/unixODBC*/README.64bit for more information
- # Use these definitions with the original unixODBC package
- # Driver from the postgresql-odbc package
- # Setup from the unixODBC-libs package
- [SQL Server]
- Description = FreeTDS ODBC driver for MSSQL
- Driver = /usr/lib64/libtdsodbc.so
- Setup = /usr/lib64/libtds.so
- FileUsage = 1
4.测试连接:
- import pyodbc
- conn=pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.1.108;port=1433;DATABASE=mydata;UID=sa;PWD=passwd')
- >>> cursor=conn.cursor()
- >>> for row in cursor.execute("select * from mytable"):
- ... print(row)
阅读(8124) | 评论(0) | 转发(0) |