我有以下带有 pymysql 的 python 代码
cursor=conn.cursor()
cursor.execute("""select count(distinct `session_id`) from innodb.fh where `cs-uri-stem` like "/detail%" """)
cursor.fetchone()
工作正常。但我只是想知道如何打印出我的 sql 语句的结果。它应该打印出 1204。我尝试了以下但它不起作用:
results = cursor.execute("""select count(distinct `session_id`) from innodb.fh where `cs-uri-stem` like "/detail%" """)
print (results)
谢谢!
最佳答案
PyMySQL
游标定义了一个名为rowcount
的属性,如source code 中所示。 .
在 cursor.execute()
之后,您可以立即调用 cursor.rowcount
以获取受 SQL 语句影响的行数。
关于Python打印SQL语句结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36380765/