简体   繁体   English

Python SQLModel - 截断表或全部删除并获取行数

[英]Python SQLModel - Truncate table or delete all and get number of rows

Using Python SQLModel, I want to truncate a table, or remove all rows, and get the number of deleted rows, in the most SQLModel standar way.使用 Python SQLModel,我想以最 SQLModel 标准的方式截断一个表,或删除所有行,并获取删除的行数。 How can I do this?我怎样才能做到这一点? I am using this:我正在使用这个:

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

But it raises an error:但它引发了一个错误:

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

I have also tried scalar() and fetchone() instead of fetchall() without success.我也尝试过scalar()fetchone()而不是fetchall()没有成功。

with Session(engine) as session:
    statement = delete(MyModel)
    result = session.exec(statement)
    session.commit()
    print(result.rowcount)

Matched Row Counts 匹配行数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM