SQL Alchemy TypeError:where()有一个意外的关键字参数

时间:2017-09-06 21:30:07

标签: python sqlalchemy

简单的Python SQLAlchemy where(),但是这个错误让我发疯了。无法理解。

In [1]: import sqlalchemy as sa
   ...: import pandas as pd
   ...: 
   ...: import etl_utils as eu

In [2]: engine = eu.set_db('s','username',input('enter password:\t'),
   ...:                    sql_database_name='DBNAME')

In [3]: meta = sa.MetaData(engine)
   ...: meta.reflect()

...编辑......

In [6]: tbl
Out[6]: Table('factSecurityPrices', MetaData(bind=Engine(mssql+pyodbc:///?odbc_connect=Driver={ODBC Driver 13 for SQL Server};Server=redacted)), Column('SecurityID_FK', INTEGER(), ForeignKey('dimSecurities.SecurityID_PK'), table=<factSecurityPrices>), Column('ImportInfoID_FK', INTEGER(), ForeignKey('dimImportInfo.ImportInfoID_PK'), table=<factSecurityPrices>), Column('PriceDate', DATETIME(), table=<factSecurityPrices>), Column('SecurityPrice', DECIMAL(precision=22, scale=4), table=<factSecurityPrices>), schema=None)
In [7]: tbl.update().where(ImportInfoID_FK=398).values(SecurityPrice=8.5)
Traceback (most recent call last):

  File "<ipython-input-7-dfa9428585e6>", line 1, in <module>
    tbl.update().where(ImportInfoID_FK=398).values(SecurityPrice=8.5)

TypeError: where() got an unexpected keyword argument 'ImportInfoID_FK'

为什么我不能将kwarg传递给where()?在线的所有示例和文档都显示您可以!

修改 尝试使用评论中的建议,没有用:

In [8]: tbl.update().where(tbl.ImportInfoID_FK=398).values(SecurityPrice=8.5)
  File "<ipython-input-8-53fb97b0906a>", line 1
    tbl.update().where(tbl.ImportInfoID_FK=398).values(SecurityPrice=8.5)
                      ^
SyntaxError: keyword can't be an expression


In [9]: tbl.update().where(tbl.c.ImportInfoID_FK=398).values(SecurityPrice=8.5)
  File "<ipython-input-9-c1ceb5a2591f>", line 1
    tbl.update().where(tbl.c.ImportInfoID_FK=398).values(SecurityPrice=8.5)
                      ^
SyntaxError: keyword can't be an expression

1 个答案:

答案 0 :(得分:3)

您需要使用.where(tbl.c.ImportInfoID_FK==398)。有关详细说明,请参阅here