如何解决 jaydebeapi.connect 命令的“第一个参数不能强制转换为字符串”?

How to solve a "1st arg can't be coerced to String" for a jaydebeapi.connect command?

在 Windows Server 2016 上,我们尝试使用 Jython 脚本通过 JDBC 进行连接,但我们的 jaydebeapi.connect 语句出现以下错误:

TypeError: getConnection(): 1st arg can't be coerced to String

然而,当我们查看 examples of using jaydebeapi 时,第一个参数 一个字符串。

这是我们的 Python 代码:

jclassname = "com.microsoft.sqlserver.jdbc.SQLServerDriver" 
database = "our_database_name"
db_elem = ";databaseName={}".format(database) if database else ""
host = "###.##.###.###" # ip address
port = "1433"    
user = "user_name"
password = "password"  
url = (jdbc:sqlserver://{host}:{port}{db_elem}"        ";user={user};password={password}".format(host=host, port=port, db_elem=db_elem,  er=user, password=password)    )    
print url
driver_args = [url]
jars = None
libs = None
db = jaydebeapi.connect(jclassname, driver_args, jars=jars, libs=libs)

这就是我们 运行 我们的 Python 脚本:

C:\jython2.7.0\bin\jython.exe C:\path_to_our_script.py

我们缺少什么?我们如何为我们的 jaydebeapi.connect 语句解决这个字符串强制错误?

来自 JayDeBeApi 文档 -

Basically you just import the jaydebeapi Python module and execute the connect method. This gives you a DB-API conform connection to the database.

The first argument to connect is the name of the Java driver class. The second argument is a string with the JDBC connection URL. Third you can optionally supply a sequence consisting of user and password or alternatively a dictionary containing arguments that are internally passed as properties to the Java DriverManager.getConnection method. See the Javadoc of DriverManager class for details.

您从 DriverManager.getConnection 方法中收到此错误。

来自 DriverManager 的 JavaDocs -

public static Connection getConnection(String url, Properties info)

所以,您的 jaydebeapi.connect 函数调用一团糟。您的第二个参数应该是 url 作为字符串。

以下是 JayDeBeApi 文档中的示例片段。

>>> import jaydebeapi
>>> conn = jaydebeapi.connect("org.hsqldb.jdbcDriver",
...                           "jdbc:hsqldb:mem:.",
...                           ["SA", ""],
...                           "/path/to/hsqldb.jar",)