gpt4 book ai didi

python - MSSQL django_pyodbc 连接问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:44 25 4
gpt4 key购买 nike

我正在尝试借助 django-pyodbc 连接到 MSSQL。我已经安装了所有必需的包,如 FreeTDS、unixODBC 和 django-pyodbc。当我使用 tsql 和 isql 连接时,我能够连接:-

>tsql -S mssql -U ********* -P ***********
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>

isql -v mssql ********* ***********
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL>

但是,当我尝试从 python 连接时,它不起作用。我遇到以下错误:-

>python
Python 2.7.14 (default, May 16 2018, 06:48:40)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyodbc;
>>> print(pyodbc.connect("DSN=GB0015APP09.dir.dbs.com;UID=*********;PWD=*************").cursor().execute("select 1"));
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
>>>

我已经检查了所有其他相关答案,但没有任何效果。不确定它正在查找哪个数据源名称。

以下是所有相关配置:-

/root>cat .freetds.conf
[mssql]
host = my_server_name
instance = MSSQL
Port = 1433
tds version =

/root>cat /etc/odbc.ini
[ServerDSN]
Driver = FreeTDS
Description = FreeTDS
Trace = No
Servername = mssql
Server = my_server_name
Port = 1433
Database = my_db_name


/root>cat /etc/odbcinst.ini
[FreeTDS]
Description = FreeTDS
Driver = /usr/lib64/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so
fileusage=1
dontdlclose=1
UsageCount=1

我已经浪费了几天时间试图解决这个问题,但没有成功。

请帮帮我。

编辑

/root>tsql -C
Compile-time settings (established with the "configure" script)
Version: freetds v0.95.81
freetds.conf directory: /etc
MS db-lib source compatibility: yes
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
OpenSSL: no
GnuTLS: yes

/root>odbcinst -j
unixODBC 2.3.1
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

最佳答案

尝试:

con = pyodbc.connect("DSN=GB0015APP09.dir.slb.com;UID=**;PWD=**;TDS_Version=7.2")
cursor = con.cursor()

results = cursor.execute("SELECT 1")
print(results)

TDS_Version=7.2 是关键。

我已经开始在 Django 中使用 MSODBC 驱动程序而不是 FreeTDS。如果您对演练感兴趣,see here .

关于python - MSSQL django_pyodbc 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52501105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com