【问题标题】:How to use SQLAlchemy reflection with Sybase? [answer: turns out it's not supported!]如何在 Sybase 中使用 SQLAlchemy 反射? [答案:原来不支持!]
【发布时间】:2012-03-24 04:56:07
【问题描述】:

我正在尝试更多地了解 Python 中的 .egg 概念和覆盖方法。这是我收到的错误消息:

Traceback (most recent call last):
  File "C:/local/work/scripts/plmr/plmr_db.py", line 42, in <module>
    insp.reflecttable(reo_daily_table, column_list)
  File "build\bdist.win32\egg\sqlalchemy\engine\reflection.py", line 370, in reflecttable
  File "build\bdist.win32\egg\sqlalchemy\engine\reflection.py", line 223, in get_columns
  File "build\bdist.win32\egg\sqlalchemy\engine\base.py", line 260, in get_columns
NotImplementedError

下面是 base.py 中的具体函数:

def get_columns(self, connection, table_name, schema=None, **kw):
    """Return information about columns in `table_name`.

    Given a :class:`.Connection`, a string
    `table_name`, and an optional string `schema`, return column
    information as a list of dictionaries with these keys:

    name
      the column's name

    type
      [sqlalchemy.types#TypeEngine]

    nullable
      boolean

    default
      the column's default value

    autoincrement
      boolean

    sequence
      a dictionary of the form
          {'name' : str, 'start' :int, 'increment': int}

    Additional column attributes may be present.
    """

    raise NotImplementedError()

所以我的问题是 - 我是否通过在我的主模块中编写一个新方法来覆盖这个函数?还是我在导入的过程中遗漏了一步?还是我完全偏离了轨道?

感谢您的任何帮助:)

编辑:添加我的代码

import sys
from sqlalchemy import create_engine, select, Table, MetaData
from sqlalchemy.engine import reflection

dbPath = 'connection_string'
engine = create_engine(dbPath, echo=True)

connection = engine.connect()

#reflect tables into memory
meta = MetaData()
reo_daily_table = Table('reo_daily',meta)
insp = reflection.Inspector.from_engine(engine)
column_list=[...]
insp.reflecttable(reo_daily_table, column_list)

connection.close()

【问题讨论】:

  • 我们对您编写的代码一无所知,因此我们不知道您哪里出错了。我们可以推测,但在我们真正看到一些代码之前(最好是一个足以重现错误的最小示例),我们所能做的就是在黑暗中拍摄。
  • 添加了我的代码,感谢您的建议
  • 问题完全错误;你不应该需要继承或覆盖那里的任何东西。您只是错误地使用了 SQLAlchemy。
  • 有了表反射,实际的数据库连接非常重要。 'connection_string' 不可能有效。此外,column_list=[...] 不是有效的 python。您可以将连接 url 的“username:password@host/db”部分替换为假人,但我们确实需要您的实际代码
  • @TokenMacGuy 我的 dbPath 是有效的,我能够很好地连接到数据库 - 不想公开发布服务器名称、用户名、密码等。至于列列表,它也是填充了有效的 Python。 e:这里是连接字符串的格式:'sybase+pysybase://user:pass@server_dsn/tablename'

标签: python sqlalchemy overriding egg


【解决方案1】:

你完全误解了。您不需要对任何东西进行子类化,而且这个问题与鸡蛋和 .ini 文件完全无关。

您不应该以这种方式实例化 Inspector。如果你读 SQLAlchemy docs 仔细,你会注意到你不应该直接使用反射构造函数;相反,你应该写

insp = reflection.Inspector.from_engine(engine)

【讨论】:

  • 我根据您的建议更新了该行;值得注意的是,当我最初编写代码时,我遵循了here (at the bottom of that section) 列出的示例。即使您建议的更改,我仍然收到相同的错误:(
  • 但是,该页面上没有示例使用 Inspector(engine) 而是 Inspector.from_engine(engine)。顺便说一句,我建议您将问题的标题更改为“如何在 Sybase 中使用 SQLAlchemy 反射”;)
【解决方案2】:

【讨论】:

  • 就是这样,感谢一群人 - 让我免于在此浪费另外 3 个小时
  • 只要您提供了足够的信息,一切都很容易解决:D
猜你喜欢
  • 1970-01-01
  • 2012-03-24
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多