如何访问受以下因素影响的行数:
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'")
尝试使用fetchone:
fetchone
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'") result=cursor.fetchone()
result将包含一个元素为的元组COUNT(*)。因此找到行数:
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()