您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python:受cursor.execute(“ SELECT?。”)影响的行数

Python:受cursor.execute(“ SELECT?。”)影响的行数

尝试使用fetchone

cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'")
result=cursor.fetchone()

result将包含一个元素为的元组COUNT(*)。因此找到行数:

number_of_rows=result[0]

或者,如果您愿意一口气做到这一点:

cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'")
(number_of_rows,)=cursor.fetchone()

PS。最好在可能的情况下使用参数化的参数,因为它可以在需要时自动为您引用参数,并防止sql注入。

参数化参数的正确语法取决于您的python /数据库适配器(例如MysqLdb,psycopg2或sqlite3)。它看起来像

cursor.execute("SELECT COUNT(*) from result where server_state= %s AND name LIKE %s",[2,digest+"_"+charset+"_%"])
(number_of_rows,)=cursor.fetchone()
python 2022/1/1 18:49:58 有354人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶