0

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() 
4
  • I think you should be testing for len(row) == 0 or row is None perhaps, you could debug this simply by just printing row to see why the else condition is never met
    – EdChum
    Commented Feb 25, 2015 at 10:53
  • 1
    This isn't valid Python. There's no object NULL (unless you've defined one yourself and haven't shown us). Commented Feb 25, 2015 at 10:54
  • NULL isn't a valid Python identifier. What error are you getting? Commented Feb 25, 2015 at 10:54
  • 1
    Thank for the replies sirs. It's suppose to be 0 not null. I updated it already Commented Feb 25, 2015 at 10:56

3 Answers 3

2

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')
1
  • It's all working now. I just acquired something new out of this. Thank you for this sir! Commented Feb 25, 2015 at 11:22
1

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
1

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:
2
  • Thanks for the reply sir. I've tried your solution but it still wouldn't execute the else statement if I place something which isn't stored in the db. Commented Feb 25, 2015 at 10:58
  • still not working sir. I just want to let you know that I am using "title" instead of "quantity" sir, if that of any help. Commented Feb 25, 2015 at 11:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.