Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69868
  • 博文数量: 12
  • 博客积分: 266
  • 博客等级: 二等列兵
  • 技术积分: 181
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-27 16:20
文章分类

全部博文(12)

文章存档

2012年(12)

我的朋友

分类: Python/Ruby

2012-10-31 15:41:55

需求:
       在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

查找出一下两行代码:


  1. extra_compile_args = None
  2. extra_link_args = None
修改为:


  1. extra_compile_args = ['-Wno-write-strings', '-I/usr/include']
  2. extra_link_args = ['-L/usr/lib']
这两个的修改主要是根据自身linux系统安装好的unixodbc包的环境进行相应配置:

查看unixODBC-devel的安装路径

  1. # rpm -ql unixODBC-devel-2.2.11-10.el5.x86_64
  2. /usr/include/autotest.h
  3. /usr/include/odbcinst.h
  4. /usr/include/odbcinstext.h
  5. /usr/include/sql.h
  6. /usr/include/sqlext.h
  7. /usr/include/sqltypes.h
  8. /usr/include/sqlucode.h
  9. /usr/include/uodbc_stats.h
  10. /usr/lib64/libboundparam.a
  11. /usr/lib64/libboundparam.so
  12. /usr/lib64/libesoobS.a
  13. /usr/lib64/libesoobS.so
  14. /usr/lib64/libgtrtst.a
  15. /usr/lib64/libgtrtst.so
  16. /usr/lib64/libmimerS.a
  17. /usr/lib64/libmimerS.so
  18. /usr/lib64/libnn.a
  19. /usr/lib64/libnn.so
  20. /usr/lib64/libodbc.a
  21. /usr/lib64/libodbccr.a
  22. /usr/lib64/libodbccr.so
  23. /usr/lib64/libodbcdrvcfg1S.a
  24. /usr/lib64/libodbcdrvcfg1S.so
  25. /usr/lib64/libodbcdrvcfg2S.a
  26. /usr/lib64/libodbcdrvcfg2S.so
  27. /usr/lib64/libodbcinst.a
  28. /usr/lib64/libodbcinst.so
  29. /usr/lib64/libodbcinstQ.a
  30. /usr/lib64/libodbcminiS.a
  31. /usr/lib64/libodbcminiS.so
  32. /usr/lib64/libodbcmyS.a
  33. /usr/lib64/libodbcnnS.a
  34. /usr/lib64/libodbcnnS.so
  35. /usr/lib64/libodbcpsql.a
  36. /usr/lib64/libodbcpsqlS.a
  37. /usr/lib64/libodbctxtS.a
  38. /usr/lib64/libodbctxtS.so
  39. /usr/lib64/liboplodbcS.a
  40. /usr/lib64/liboplodbcS.so
  41. /usr/lib64/liboraodbcS.a
  42. /usr/lib64/liboraodbcS.so
  43. /usr/lib64/libsapdbS.a
  44. /usr/lib64/libsapdbS.so
  45. /usr/lib64/libtdsS.a
  46. /usr/lib64/libtdsS.so
  47. /usr/lib64/libtemplate.a
  48. /usr/lib64/libtemplate.so

3.配置odbc

增加配置

  1. # more /etc/odbcinst.ini
  2. # Example driver definitions
  3. # See /usr/share/doc/unixODBC*/README.64bit for more information
  4. # Use these definitions with the original unixODBC package
  5. # Driver from the postgresql-odbc package
  6. # Setup from the unixODBC-libs package
  7. [SQL Server]
  8. Description = FreeTDS ODBC driver for MSSQL
  9. Driver = /usr/lib64/libtdsodbc.so
  10. Setup = /usr/lib64/libtds.so
  11. FileUsage = 1

4.测试连接:

测试

  1. import pyodbc
  2. conn=pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.1.108;port=1433;DATABASE=mydata;UID=sa;PWD=passwd')
  3. >>> cursor=conn.cursor()
  4. >>> for row in cursor.execute("select * from mytable"):
  5. ...     print(row)






 

阅读(8124) | 评论(0) | 转发(0) |
0

上一篇:那些年追女孩的曾经

下一篇:samba实例

给主人留下些什么吧!~~