gpt4 book ai didi

python - 使用 sqlalchemy 获取插入行的数量

转载 作者:行者123 更新时间:2023-12-05 06:26:55 24 4
gpt4 key购买 nike

我正在编写代码来更新我的数据库,我想获取提交后插入的行数,我该怎么做?

for table, endpoint in ENDPOINTS.items():
url = BASE_URL + endpoint
response = get_response(url)

table_object = getattr(Base.classes, table)

self.add_missing_data(response, table_object, session)

session.commit()
print('Total rows added: {}'.format(xxxxx))

最佳答案

引用this Q/A :

这是你的代码:

from sqlalchemy import create_engine

def add_mising_data(self, response, table_object, session):
con = create_engine(...)
# insert some data using
# con, response, table_object and session
result_proxy = con.execute("INSERT ...")
return result_proxy.rowcount # this is the number of inserted rows

# in your loop
total_num_row = 0
for table, endpoint in ENDPOINTS.items():
url = BASE_URL + endpoint
response = get_response(url)

table_object = getattr(Base.classes, table)

num_row = self.add_missing_data(response, table_object, session)
total_num_row = total_num_row + num_row

print (total_num_row) # <-- here you are

关于python - 使用 sqlalchemy 获取插入行的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699682/

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