我正在尝试使用包JayDeBeApi连接到Python 3中的Teradata数据库。这是使用Flask和Flask-Restplus的 API 项目的一部分
这是重现该问题的最小工作示例。在终端窗口中,键入以下命令来设置您的工作站:
# Install JVM
sudo apt-get install default-jre
# Create Python virtual environment
sudo apt-get install python3-venv
python3 -m venv jdbc
source jdbc/bin/activate
# Install Python packages in virtual environment
pip3 install --upgrade pip
pip3 install jaydebeapi
pip3 install flask
创建一个包含以下内容的文件 app.py:
from flask import Flask
import jaydebeapi
app = Flask(__name__)
def get_jdbc_connection():
connection = jaydebeapi.connect(
'com.teradata.jdbc.TeraDriver',
'jdbc:teradata://edw-dev.company.org',
{'user': 'LOGIN', 'password': 'PASSWORD', 'tmode': 'TERA', 'charset': 'UTF8'},
'/home/alexis/teradataDriverJdbc.jar')
return connection
@app.route('/hello/')
def hello_world():
print('Init second connection')
get_jdbc_connection()
print('Success')
return 'Hello world!'
if __name__ == '__main__':
print('Init connection')
test_connection = get_jdbc_connection()
test_connection.close()
print('Init connection closed')
app.run(host='localhost', port=5000, threaded=True, debug=True)
在激活了虚拟环境的终端窗口中,键入以下内容:
python3
>>> import app
>>> app.get_jdbc_connection()
<jaydebeapi.Connection object at 0x7f28ecb2ebe0>
上面的代码片段有效,该方法get_jdbc_connection()
返回一个连接对象。现在尝试使用 Flask 运行它。在激活了虚拟环境的终端窗口中,键入以下内容:
python3 app.py
Init connection
Init connection closed
* Running on http://localhost:5000/ (Press CTRL+C to quit)
* Restarting with stat
Init connection
Init connection closed
* Debugger is active!
* Debugger PIN: 301-429-987
在您的浏览器中,导航到http://localhost:5000/hello/ 您将收到以下错误消息,这与我在上面的第一条消息中粘贴的相同:
Init second connection
127.0.0.1 - - [08/Mar/2018 16:00:30] "GET /hello/ HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/alexis/app.py", line 19, in hello_world
get_jdbc_connection()
File "/home/alexis/app.py", line 12, in get_jdbc_connection
'/home/alexis/teradataDriverJdbc.jar')
File "/home/alexis/dq/lib/python3.5/site-packages/jaydebeapi/__init__.py", line 381, in connect
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
File "/home/alexis/dq/lib/python3.5/site-packages/jaydebeapi/__init__.py", line 199, in _jdbc_connect_jpype
return jpype.java.sql.DriverManager.getConnection(url, *dargs)
jpype._jexception.java.sql.SQLExceptionPyRaisable: java.sql.SQLException: No suitable driver found for jdbc:teradata://edw-dev.company.org
备注:我还在 Github 项目上创建了一个问题:https ://github.com/baztian/jaydebeapi/issues/66
编辑:根据 baztian 的建议,我在 jPype Github 项目上提出了这个问题:https ://github.com/originell/jpype/issues/290