0
 query = "SELECT car FROM cars....."

 cursor.execute(query)

 results = cursor.fetchall()
 for row in results:
      if not query:
           print 'No cars available."
      else:
           print row[0]

What I'm trying to do is print a message if results are null, or print the results if they have entries. The above code is what I have so far but obviously isn't working. Any suggestions?

Thanks in advance.

2
  • 4
    query is a string defined at the top, so if not query: will never succeed. Try testing row instead? Commented Oct 8, 2013 at 3:12
  • 1
    Also, you open your 'No cars available." with a single quote, but close with a double quote Commented Oct 8, 2013 at 3:50

1 Answer 1

3

If you're trying to print "No cars available" when there are no results:

 query = "SELECT car FROM cars....."

 cursor.execute(query)

 results = cursor.fetchall()
 if results:
      for row in results:
           print row[0]
 else:
      print 'No cars available.'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.