0

我有以下代码:

import pyodbc

cnxn = pyodbc.connect("DRIVER={SQL Server};"
                      +"SERVER=something.example.com;"
                      +"DATABASE=something;")

cursor = cnxn.cursor()

name=('Smith, Joe', )

cursor.execute('SELECT id FROM Users WHERE displayname=?', name)
rows = cursor.fetchall()

for row in rows:
        print row

代码在 windows/python2.7 上根据需要执行。但是,当我尝试在 linux 上运行它时,出现以下错误:

Traceback (most recent call last):
  File "/something/script.py", line 125, in <module>
    main()
  File "/something/script.py", line 77, in main
    +"DATABASE=something;")
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnectW)')

回溯似乎表明该DRIVER条目丢失,事实并非如此。这是版本差异吗?pyodbc有什么问题?

编辑:/etc/odbcinst.ini 的内容:

# Example driver definitions

# Driver from the postgresql-odbc package
# Setup from the unixODBC package
[PostgreSQL]
Description     = ODBC for PostgreSQL
Driver          = /usr/lib/psqlodbc.so
Setup           = /usr/lib/libodbcpsqlS.so
Driver64        = /usr/lib64/psqlodbc.so
Setup64         = /usr/lib64/libodbcpsqlS.so
FileUsage       = 1


# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description     = ODBC for MySQL
Driver          = /usr/lib/libmyodbc5.so
Setup           = /usr/lib/libodbcmyS.so
Driver64        = /usr/lib64/libmyodbc5.so
Setup64         = /usr/lib64/libodbcmyS.so
FileUsage       = 1
4

1 回答 1

1

您没有为 sql server 配置 odbc 驱动程序 - 您需要安装和配置一个。unixodbc网页上的驱动程序部分建议使用freetds,或者您也可以尝试微软自己的实现

freedts 应该可以使用yum.

基本配置应如下所示:

/etc/odbcinst.ini

[SQL Server]
Description = FreeTDS driver for SQL Server
Driver = /usr/lib/libtdsodbc.so
Driver64 = /usr/lib64/libtdsodbc.so

现在您应该已经可以连接了。有关配置的更多详细信息,请查看此处

编辑:

或者,还有其他方法可以从 python 连接到 sql 服务器,例如python-tdspymssql等等......

于 2013-08-20T21:56:37.353 回答