我有一个表,其中包含 URL 列表以及它们被访问的次数。使用 python,如何创建一个 SQL 查询来增加特定 URL 的 HITS 列?即特定行?
到目前为止我已经:
def insertURL(url) :
##query to fetch all URLs
sql = "SELECT URL_visited FROM URL"
##query to insert a number into the HITS column
insert_sql = "INSERT INTO URL_visited (total_hits)"
insert_sql = insert_sql + "VALUES('"+1+"')"
##execute sql query to fetch URLs
cursor.execute(sql)
results = cursor.fetchall()
##flag to check whether the URL already exists in table
flag = 0
##checking if URL exists
for row in results:
if (row==url) :
flag = 1
##if yes, increment HITS column for that URL (specific row)
if flag == 1 :
##how to do??
如您所见,我可以在 HITS 列中插入一个数字,但如何增加特定行的值?
最佳答案
优化方法:只需更新/增量特定url_visited
值的total_hits
列值:
(考虑urls
是目标表名称)
def update_url_hits(url):
sql_query = "UPDATE urls SET total_hits = total_hits + 1 WHERE url_visited = %"
cursor.execute(sql_query, (url,))
connection.commit()
关于python - 增加特定行的列中的整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57430164/