Python, how to check if a result set is empty?

后端 未结 7 1208
予麋鹿
予麋鹿 2021-02-02 05:26

I have a sql statement that returns no hits. For example, \'select * from TAB where 1 = 2\'.

I want to check how many rows are returned,

cu         


        
7条回答
  •  粉色の甜心
    2021-02-02 06:04

    MySQLdb will not raise an exception if the result set is empty. Additionally cursor.execute() function will return a long value which is number of rows in the fetched result set. So if you want to check for empty results, your code can be re-written as

    rows_count = cursor.execute(query_sql)
    if rows_count > 0:
         rs = cursor.fetchall()
    else:
         // handle empty result set
    

提交回复
热议问题