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

后端 未结 7 1204
予麋鹿
予麋鹿 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:10

    I had issues with rowcount always returning -1 no matter what solution I tried.

    I found the following a good replacement to check for a null result.

    c.execute("SELECT * FROM users WHERE id=?", (id_num,))
    row = c.fetchone()
    if row == None:
       print("There are no results for this query")
    

提交回复
热议问题