gpt4 book ai didi

python - flask : number of rows updated

转载 作者:行者123 更新时间:2023-12-01 01:51:23 27 4
gpt4 key购买 nike

如何获取 db.session.commit() 更新/添加的行数在 flask 中?

如果我尝试:

    for row in csv_data:
ticket_num = row[0]

# Generate random UUID token string
token = uuid.uuid4()

ticket = Ticket(token, ticket_num, None, None, batch_num)
db.session.add(ticket)

num = db.session.commit()
if num:
flash(str(num) + ' tickets were uploaded/updated.')

我没有收到任何反馈。

最佳答案

session 有一些属性,您可以检查数据库中将发生什么。 session 有 new: 用于对象,它将被添加到数据库中,以及 dirty: 用于对象,它将被更新:

t1 = Ticket(token, ticket_num, None, None, batch_num)
t2 = Ticket(token, ticket_num, None, None, batch_num)
t3 = Ticket.query.filter_by(token='Something to match').first()
t3.token = 'Something new'
db.session.add_all([t1, t2, t3])

# check for new
>>> db.session.new
IdentitySet([<Ticket (transient 140452313747240)>, <Ticket (transient 140452278620112)>])
# check updates
>>> db.session.dirty
>>>db.session.dirty
IdentitySet([<Ticket 2>]) # 2 is ticket id for me

您可以检查类(class)长度:len(db.session.new)

请注意,这在提交之前可用,但您将知道添加和更新了多少项目。

关于python - flask : number of rows updated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50688959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com