gpt4 book ai didi

python-3.x - 在 Python 中创建 jTDS 连接字符串

转载 作者:行者123 更新时间:2023-12-02 00:20:56 30 4
gpt4 key购买 nike

我正在尝试使用 Python 建立 jTDS 连接。谁能帮我实现这个目标?

在 SQL Server 中,我通常使用以下连接字符串进行连接:

jdbc:jtds:sqlserver://DBServer:port/DBInstance;useNTLMv2=true;domain=Domain

我正尝试在 Python 中执行以下操作:

import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=DBName,port;'
'Database=database;'
'Truster_Connection=yes;'
'uid=user;'
'pwd=password;'
)

cursor = conn.cursor()
cursor.execute('SELECT * from Table')

for row in cursor:
print(row)

错误:

pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'USER'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'USER'. (18456); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)"))

最佳答案

如果您专门尝试使用 jTDS 而不是 ODBC 连接到 MS SQL Server,那么您可以使用 jaydebeapi python 包。

请参阅以下代码(Python 3):

import sys
import jaydebeapi


def main():
try:
# jTDS Driver.
driver_name = "net.sourceforge.jtds.jdbc.Driver"

# jTDS Connection string.
connection_url = "jdbc:jtds:sqlserver://<server_hostname>:<port>/<database_name>"

# jTDS Connection properties.
# Some additional connection properties you may want to use
# "domain": "<domain>"
# "ssl": "require"
# "useNTLMv2": "true"
# See the FAQ for details http://jtds.sourceforge.net/faq.html
connection_properties = {
"user": "username",
"password": "password",
}

# Path to jTDS Jar
jar_path = "<path_to>\\jtds-1.3.1.jar"

# Establish connection.
connection = jaydebeapi.connect(driver_name, connection_url, connection_properties, jar_path)
cursor = connection.cursor()

# Execute test query.
cursor.execute("select 1 as test_connection")
res = cursor.fetchall()
if res:
print(str(res)) # Should print [(1,)]

except Exception as err:
print(str(err))


if __name__ == "__main__":
sys.exit(main())

在此之前,您需要完成以下工作:

  1. here 下载并安装 JDK/JRE
  2. pip install jaydebeapi 或从 here 下载
  3. here 下载 jtds
  4. 更新 connection_url、connection_properties、jar_path。

关于python-3.x - 在 Python 中创建 jTDS 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55376100/

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