gpt4 book ai didi

python - 异常值 : ('08001' , '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)' )

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:31 26 4
gpt4 key购买 nike

我正在使用 pyodbc 库从在 ubuntu 虚拟机上运行的 python djanog 网络应用程序连接到 windows 上的远程 sql server 实例。

我有一个数据库连接类,如下所示,它在我尝试创建对象连接的那一行中断(我一直在尝试许多连接字符串);

import pyodbc


class SQLSeverConnection():

def __init__(self, DSN, user, password, database):
connectionString = 'DSN=MSSQLServerDataSource;UID=django;PWD=password123!;DATABASE=HD'

self.connection = pyodbc.connect(connectionString)
self.cursor = self.connection.cursor()

def getTableNames(self):
self.cursor.execute('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = \'BASE TABLE\'')
tables = self.cursor.fetchall()
return tables

def getColumnTitles(self, tableName):
self.cursor.execute("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + tableName + "' ORDER BY ORDINAL_POSITION")
columns = self.cursor.fetchall()
return columns

def getColumnData(self, columnName, tableName, startDateTime, endDateTime):
self.cursor.execute('SELECT ' + columnName + ' FROM ' + tableName + ' BETWEEN ' + startDateTime + ' AND ' + endDateTime + ' ORDER BY timestamp')
data = self.cursor.fetchall()
return data

当我运行服务器时,出现标题中所述的错误。

我的配置文件如下;

(odbc.ini)
[MSSQLServerDataSource]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = MSSQLServer
Database = HD
TDS_Version = 8.0

(odbcinst.ini)
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
# Some installations may differ in the paths
#Driver = /usr/lib/odbc/libtdsodbc.so
#Setup = /usr/lib/odbc/libtdsS.so
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 4

(freetds.conf)
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".

# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
; tds version = 4.2

# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff

# Command and connection timeouts
; timeout = 10
; connect timeout = 10

# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512

#Server For Django App
[MSSQLSever]
host = <ip>
port = 1433
tds version = 8.0

当我在终端中输入 odbcinst -j 时;

unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/user/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

使用 isql 和 dsn、用户和密码从命令行成功连接。

我真的不知道该怎么办,我一直在兜圈子一天多了。一些帮助将不胜感激!

最佳答案

试试这个:

connectionString = 'DRIVER={FreeTDS};SERVER=10.1.3.230;PORT=1433;DATABASE=HD;UID=django;PWD=password123!;TDS_Version=7.2;'

您需要明确指定 TDS 版本。在这里查看您可以使用的不同 TDS 版本,7.2 在 2014 年之前的每个 SQL Server 上为我工作了 2008+:

http://www.freetds.org/userguide/choosingtdsprotocol.htm

您需要在 Django pyodbc 设置中执行相同的操作。我建议使用此版本的 django-pyodbc:

https://github.com/lionheart/django-pyodbc/

祝你好运,让我知道它是否适合你。

关于python - 异常值 : ('08001' , '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24906016/

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