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

后端 未结 7 1206
予麋鹿
予麋鹿 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

    You can do like this :

    count = 0
    cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                          "Server=serverName;"
                          "Trusted_Connection=yes;")
    cursor = cnxn.cursor()
    cursor.execute(SQL query)
    for row in cursor:
        count = 1
        if true condition:
            print("True")
        else:
            print("False")
    if count == 0:
        print("No Result")
    

    Thanks :)

提交回复
热议问题