【问题标题】:Python SQLModel - Truncate table or delete all and get number of rowsPython SQLModel - 截断表或全部删除并获取行数
【发布时间】:2021-12-11 21:57:38
【问题描述】:

使用 Python SQLModel,我想以最标准的 SQLModel 方式截断表,或删除所有行,并获取删除的行数。我怎样才能做到这一点?我正在使用这个:

        with Session(engine) as session:
            count = session.exec(delete(MyModel)).fetchall()
            session.commit()

但它会引发错误:

ResourceClosedError('This result object does not return rows. It has been closed automatically.')

我也试过scalar()fetchone()而不是fetchall()但没有成功。

【问题讨论】:

    标签: python sqlalchemy sqlmodel


    【解决方案1】:
    with Session(engine) as session:
        statement = delete(MyModel)
        result = session.exec(statement)
        session.commit()
        print(result.rowcount)
    

    Matched Row Counts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多