Python打印SQL语句结果

标签 python mysql sql python-2.7 python-3.x

我有以下带有 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/

相关文章:

python - 如何根据 python 中 2 列的条件创建具有值的新数据框列?

python - 如何在串联文件之间添加空的新行?

php webservice不能同时处理多个请求

SQL DATEDIFF 返回错误值

如果任一匹配查询,MySQL 查询将返回 2 个值的结果

python - Pandas - 根据另一个列表中的索引对列表中的值求和

python - 保存python argparse文件

mysql - Kohana DB - 使用 SQL_CACHE

mysql - 在mysql中将varchar字段作为整数排序

php - 如何获取magento表名