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

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

    if you're connecting to a postgres database, the following works:

    result = cursor.execute(query)
    
    if result.returns_rows:
        # we got rows!
        return [{k:v for k,v in zip(result.keys(), r)} for r in result.rows]
    else:
        return None
    

提交回复
热议问题