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

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

    0 讨论(0)
提交回复
热议问题