0

我目前能够使用我编译并与 pyodbc 一起使用的 denodo 驱动程序连接到 denodo db,如果我直接使用 psycopg2,我还可以连接和查询表。但是,如果我可以使用 Sqlalchemy 的 ORM,那将是理想的。该驱动程序基于 postgresql,但目前 sqlalchemy 不支持方言和 DBAPI 的组合。这导致我尝试使用指向我编译的驱动程序的方言 mysql+pyodbc 以及尝试 psycopg2。

我应该提到我的意图是只对数据库中的一个表执行 SELECT 语句。

使用 mysql+pyodbc 我得到错误:

qlalchemy.engine.base.Engine.DENODO-READER b"SHOW VARIABLES LIKE 'character_set%%'"
2020-06-21 02:56:52,972 INFO sqlalchemy.engine.base.Engine.DENODO-READER ()
[2020-06-21 02:56:52 +0000] [8] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/decomm_reader/wsgi.py", line 5, in <module>
    APP = create_app()
  File "/home/decomm_reader/app/__init__.py", line 33, in create_app
    DB.metadata.reflect(bind=DB.engine)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/sql/schema.py", line 4356, in reflect
    with bind.connect() as conn:
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2272, in connect
    return self._connection_cls(self, **kwargs)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 104, in __init__
    else engine.raw_connection()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2379, in raw_connection
    self.pool.unique_connection, _connection
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2345, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 304, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 495, in checkout
    rec = pool._do_get()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get
    self._dec_overflow()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
    exc_value, with_traceback=exc_tb,
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get
    return self._create_connection()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 440, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 666, in __connect
    ).exec_once_unless_exception(self.connection, self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 314, in exec_once_unless_exception
    self._exec_once_impl(True, *args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 285, in _exec_once_impl
    self(*args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 322, in __call__
    fn(*args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 1513, in go
    return once_fn(*arg, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 199, in first_connect
    dialect.initialize(c)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/mysql/base.py", line 2472, in initialize
    self._connection_charset = self._detect_charset(connection)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/mysql/pyodbc.py", line 89, in _detect_charset
    rs = connection.execute("SHOW VARIABLES LIKE 'character_set%%'")
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 1012, in execute
    return self._execute_text(object_, multiparams, params)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 1187, in _execute_text
    parameters,
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 1324, in _execute_context
    e, statement, parameters, cursor, context
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 1521, in _handle_dbapi_exception
    util.raise_(exc_info[1], with_traceback=exc_info[2])
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 1284, in _execute_context
    cursor, statement, parameters, context
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
TypeError: The first argument to execute must be a string or unicode query.

使用 postgres+psycopg2 我得到以下异常:

[2020-06-21 03:03:01 +0000] [8] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2345, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 304, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 495, in checkout
    rec = pool._do_get()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get
    self._dec_overflow()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
    exc_value, with_traceback=exc_tb,
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get
    return self._create_connection()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 440, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 666, in __connect
    ).exec_once_unless_exception(self.connection, self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 314, in exec_once_unless_exception
    self._exec_once_impl(True, *args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 285, in _exec_once_impl
    self(*args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 322, in __call__
    fn(*args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 189, in on_connect
    do_on_connect(conn)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 865, in on_connect
    fn(conn)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 832, in on_connect
    hstore_oids = self._hstore_oids(conn)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 904, in oneshot
    result = fn(self, *args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 922, in _hstore_oids
    oids = extras.HstoreAdapter.get_oids(conn)
  File "/usr/local/lib64/python3.7/site-packages/psycopg2/extras.py", line 929, in get_oids
    """ % typarray)
psycopg2.DatabaseError: View 'pg_type' not found
DETAIL:  java.sql.SQLException: View 'pg_type' not found


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/decomm_reader/wsgi.py", line 5, in <module>
    APP = create_app()
  File "/home/decomm_reader/app/__init__.py", line 33, in create_app
    DB.metadata.reflect(bind=DB.engine)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/sql/schema.py", line 4356, in reflect
    with bind.connect() as conn:
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2272, in connect
    return self._connection_cls(self, **kwargs)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 104, in __init__
    else engine.raw_connection()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2379, in raw_connection
    self.pool.unique_connection, _connection
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2349, in _wrap_pool_connect
    e, dialect, self
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 1591, in _handle_dbapi_exception_noconnection
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/base.py", line 2345, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 304, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 495, in checkout
    rec = pool._do_get()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/impl.py", line 140, in _do_get
    self._dec_overflow()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
    exc_value, with_traceback=exc_tb,
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/impl.py", line 137, in _do_get
    return self._create_connection()
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 309, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 440, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/pool/base.py", line 666, in __connect
    ).exec_once_unless_exception(self.connection, self)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 314, in exec_once_unless_exception
    self._exec_once_impl(True, *args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 285, in _exec_once_impl
    self(*args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/event/attr.py", line 322, in __call__
    fn(*args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 189, in on_connect
    do_on_connect(conn)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 865, in on_connect
    fn(conn)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 832, in on_connect
    hstore_oids = self._hstore_oids(conn)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 904, in oneshot
    result = fn(self, *args, **kw)
  File "/usr/local/lib64/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 922, in _hstore_oids
    oids = extras.HstoreAdapter.get_oids(conn)
  File "/usr/local/lib64/python3.7/site-packages/psycopg2/extras.py", line 929, in get_oids
    """ % typarray)
sqlalchemy.exc.DatabaseError: (psycopg2.DatabaseError) View 'pg_type' not found
DETAIL:  java.sql.SQLException: View 'pg_type' not found

(Background on this error at: http://sqlalche.me/e/4xp6)
4

1 回答 1

0

来自文档:“SQLAlchemy 旨在使用为特定数据库构建的 DBAPI 实现进行操作,并包括对最流行数据库的支持。”</p>

PostgreSQL MySQL SQLite Oracle Microsoft SQL Server Firebird Sybase 还有许多其他外部支持的驱动程序,例如 DB2、Snowflake 和 redshift。使用 SQLAlchemy 连接到 Denodo 需要开发和维护一种新的方言。据我所知,不支持任何 jdbc 驱动程序。

于 2020-06-21T04:16:11.910 回答