charlie9495
charlie9495

Reputation: 87

If Else condition on python

I am trying to place a condition after the for loop. It will print the word available if the retrieved rows is not equal to zero, however if I would be entering a value which is not stored in my database, it will return a message. My problem here is that, if I'd be inputting value that isn't stored on my database, it would not go to the else statement. I'm new to this. What would be my mistake in this function?

  def search(title):           
    query = "SELECT * FROM books WHERE title = %s"
    entry = (title,)

    try:
        conn = mysql.connector.connect(user='root', password='', database='python_mysql') # connect to the database server
        cursor = conn.cursor()

        cursor.execute(query, entry)
        rows = cursor.fetchall()

        for row in rows:
            if row != 0:
                print('Available')
            else:
                print('No available copies of the said book in the library')


    except Error as e:
        print(e)

    finally:
        cursor.close()
        conn.close()

def main():
    title = input("Enter book title: ")
    search(title) 

if __name__ == '__main__':
    main() 

Upvotes: 0

Views: 4854

Answers (3)

Daniel Roseman
Daniel Roseman

Reputation: 599470

Quite apart from the 0/NULL confusion, your logic is wrong. If there are no matching rows, you won't get a 0 as the value of a row; in fact you won't get any rows at all, and you will never even get into the for loop.

A much better way to do this would be simply run a COUNT query, get the single result with fetchone(), and check that directly.

query = "SELECT COUNT(*) FROM books WHERE title = %s"
entry = (title,)

try:
    conn = mysql.connector.connect(user='root', password='', database='python_mysql') # connect to the database server
    cursor = conn.cursor()

    cursor.execute(query, entry)
    result = cursor.fetchone()


    if result != 0:
        print('Available')
    else:
        print('No available copies of the said book in the library')

Upvotes: 2

Anurag
Anurag

Reputation: 3114

In python you should check for None not NULL. In your code you can just check for object, if it is not None then control should go inside if otherwise else will be executed

for row in rows:
    if row:
        print('Available')
    else:
        print('No available copies of the said book in the library')

UPDATE after auther edited the question:

Now in for loop you should check for column value not the whole row. If your column name is suppose quantity then if statement should be like this

if row["quantity"] != 0:

Upvotes: 1

mikebutrimov
mikebutrimov

Reputation: 365

First of all NULL in python is called None. Next: according to documentation: "The method fetches all (or all remaining) rows of a query result set and returns a list of tuples. If no more rows are available, it returns an empty list. " enpty list is not None

>>> row = []
>>> row is None
False

So you need to redesign your if statment in the way like this:

for i in rows:
    if i:
        blah-blah
    else:
        blah-blah-blah

Upvotes: 1

Related Questions