Python SQLModel - 截断 table 或全部删除并获取行数
Python SQLModel - Truncate table or delete all and get number of rows
使用 Python SQLModel,我想截断一个 table,或者删除所有行,并以最 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()
但没有成功。
with Session(engine) as session:
statement = delete(MyModel)
result = session.exec(statement)
session.commit()
print(result.rowcount)
使用 Python SQLModel,我想截断一个 table,或者删除所有行,并以最 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()
但没有成功。
with Session(engine) as session:
statement = delete(MyModel)
result = session.exec(statement)
session.commit()
print(result.rowcount)