【问题标题】:Search a Set in SQLAlchemy query在 SQLAlchemy 查询中搜索集合
【发布时间】:2014-02-15 16:44:29
【问题描述】:

我在完全不同的服务器中有 2 个数据库,两者之间没有关系。两个数据库中的一列具有相同的数据。我需要根据该数据进行匹配,以便我可以在一个视图中从两个数据库中获取其他信息。

这是我在views.py 中的内容

@view_config(route_name='cis', renderer='templates/cis.pt')
def cis(request):
    db1 = cis_db.query(site.site_id).join(site_tag).filter(site_tag.tag_id.like(202)).all()
    db2 = DBSession.query(A_School.cis_site_id).all()
    sites = set(db1).intersection(db2)
    return {
        'newsites': cis_db.query(site_tag).filter(site_tag.tag_id.like(202)).count(),
        'schools': DBSession.query(table).all(),
        'test': DBSession.query(table.cis_site_id.like(sites)).all(),
        }

我的渲染页面返回此错误:

ProgrammingError: (ProgrammingError) ('Invalid parameter type.  param-index=0 param-type=set', 'HY105')

下面的 sql 代码中包含正确的数字。我认为问题在于sites 中返回的内容。以下是该错误后页面立即返回的内容:

u'SELECT [A_School].cis_site_id LIKE ? AS anon_1 
FROM [A_School]' (set([(1,), (2,), (3,), (4,), (5,)]),)

所以返回的数据看起来是正确的,但我认为领先的 set 正在抛出 .like 部分中具有 sites 的查询。不知道如何使它正常工作。

【问题讨论】:

    标签: python sqlalchemy pyramid


    【解决方案1】:

    您应该使用in_(...) 而不是like(sites)
    实际上,in_([s[0] for s in sites]) 是为了从站点集中解开元组。

    【讨论】:

    • 收到以下错误:ProgrammingError: (ProgrammingError) ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'IN'. (156) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)")
    • 你能把生成的SQL 语句分享一下吗?
    • 不知道为什么它会加载一大堆问号。 u'SELECT [A_School].cis_site_id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) AS anon_1 FROM [A_School]' (14639, 14587, 14966, 14625, 14589, 15144, 15171, 14590, 15134, 14719, 15150, 14704, 15153, 15096, 15133, 14036) 在 A_School 部分之后返回正确的数字。
    • 问号是参数是如何传递给引擎的。它的完成方式取决于驱动程序支持的内容。
    • 似乎无法在此处获取语法。有什么想法吗?司机是pyodbc
    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 2020-04-23
    • 2020-08-25
    • 1970-01-01
    • 2014-03-13
    • 2022-01-04
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多