Python: Number of rows affected by cursor.execute("SELECT …)
2017-09-25 17:28
302 查看
Try using
hold a tuple with one element, the value of
So to find the number of rows:
Or, if you'd rather do it in one fell swoop:
PS. It's also good practice to use parametrized arguments whenever possible, because it can automatically quote arguments for you when needed, and protect against sql injection.
The correct syntax for parametrized arguments depends on your python/database adapter (e.g. mysqldb, psycopg2 or sqlite3). It would look something like
fetchone:
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'") result=cursor.fetchone()
resultwill
hold a tuple with one element, the value of
COUNT(*).
So to find the number of rows:
number_of_rows=result[0]
Or, if you'd rather do it in one fell swoop:
cursor.execute("SELECT COUNT(*) from result where server_state='2' AND name LIKE '"+digest+"_"+charset+"_%'") (number_of_rows,)=cursor.fetchone()
PS. It's also good practice to use parametrized arguments whenever possible, because it can automatically quote arguments for you when needed, and protect against sql injection.
The correct syntax for parametrized arguments depends on your python/database adapter (e.g. mysqldb, psycopg2 or sqlite3). It would look something like
cursor.execute("SELECT COUNT(*) from result where server_state= %s AND name LIKE %s",[2,digest+"_"+charset+"_%"]) (number_of_rows,)=cursor.fetchone()
shareimprove this answer https://stackoverflow.com/questions/2511679/python-number-of-rows-affected-by-cursor-executeselect |
相关文章推荐
- Solution for error "Store update, insert, or delete statement affected an unexpected number of rows"
- Solution for error "Store update, insert, or delete statement affected an unexpected number of rows" .
- Solution for Store update, insert, or delete statement affected an unexpected number of rows (0)
- poi中getPhysicalNumberOfRows()和getLastRowNum()区别
- [Err] 1222 - The used SELECT statements have a different number of columns
- redis报错“max number of clients reached"
- mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonag
- Caused by: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 44; 值为 "none" 的属性 "parallel"
- The Zen of Python, by Tim Peters
- ORA-12012: error on auto execute of job "SYS"."BSLN_MAINTAIN_STATS_JOB" ORA-01950: no privileges on
- git出错"Your branch is ahead of 'origin/LS3A_2H' by 4 commits"解决
- "Out of Memory" error message appears when you have a large number of programs running
- #1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i
- MyEclipse 8.6 出现"Caused by: java.lang.OutOfMemoryError: PermGen space"解决
- mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
- poi操作Excel 被bug坑了getLastRowNum,getPhysicalNumberOfRows,getPhysicalNumberOfCells,getLastCellNum
- Fibonacci Number Established the Environment of Python
- [置顶] "On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to ..."解决
- The Zen of Python, by Tim Peters
- 1. CountDiv 数数有几个 Compute number of integers divisible by k in range [a..b].