python - 如何使用计数器计算表中插入记录的数量并使用python脚本打印每个作业中插入记录的数量
问题描述
我想使用计数器或任何方法计算表中插入记录的数量,并打印使用 python 脚本运行的每个作业中插入记录的计数。示例代码如下所示..请建议一些相同的解决方案..
with Postgresql.Database(connection_pool) as db:
--count number of rows inserted in bazaarvoicereviews table and print the count for every job run
db.insert('externaldb.bazaarvoicereviews',data=rdata)
--count number of rows inserted in bazaarvoicereviewsresponses table and print the count for every job run
for rs in _ClientResponses:
db.insert('externaldb.bazaarvoicereviewsresponses',data=rs)
--count number of rows inserted in bazaarvoicereviewscomments table and print the count for every job run
for rc in _ClientComments:
db.insert('externaldb.bazaarvoicereviewscomments',data=rc)
解决方案
如果使用psycopg2
,则可以从光标中获取行数:
cursor.execute(statement)
rowcount = cursor.rowcount
conn.commit()
完整示例:
import psycopg2
class CustomPostgres:
def __init__(self):
self.conn_string = "connection_string"
self.conn = psycopg2.connect(self.conn_string)
self.cursor = self.conn.cursor()
def insert_with_rowcount(self, statement):
self.cursor.execute(statement, params)
rowcount = self.cursor.rowcount
self.conn.commit()
return rowcount
推荐阅读
- c - 为什么我的跨平台 C 应用程序在 Windows 中释放内存时会崩溃?
- css - 如何从文件中加载动态 CSS 变量?
- python - LSTM 仅根据标签和类型预测电影 ID?
- javascript - 如何用JS元素填写VALUE
- c# - 如何使在一定时间后,用户应该自动注销?
- corda - 在任何一个控制台中出价时,我在 cordapp-auction 项目中遇到此错误
- maven - 将从xyz url下载的jar直接添加到gitlab(gitlab-ci.yml)神器中
- cmake - 是否附加了目标?
- unicode - 我在哪里可以获得 1/x 作为单个字符,就像 ½ 一样?
- django - Django 的 get_current_language 奇怪的行为