Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:51:58+00:00 2026-06-17T11:51:58+00:00

How are you supposed to handle exceptions when there are no rows for this

  • 0

How are you supposed to handle exceptions when there are no rows for this id. How should the calling code handle this

  def function(id)
    mysql_conn = MySQLdb.connect(..)    
    cursor = mysql_conn.cursor()
    cursor.execute("""select val from fooo where id = %s""", (id))
    row = cursor.fetchone()
    return row[0]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:51:59+00:00Added an answer on June 17, 2026 at 11:51 am

    Whenever I find myself fetching just one row I always like to use the following (although not the best performance wise) construct.

    cursor.execute("SELECT * FROM foo;")
    for row in cursor:
        return row
    # No row handling behind the loop.
    return "No rows found"
    

    This effectively returns when the first iteration of the loop runs. Which can only happen if there is at least 1 row in the result set. And otherwise will run the code behind the for loop.

    You can raise a custom exception, or return a special value such as None that lets the calling function know nicely that there were no rows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to 'detect' what exceptions function/method raises? Examplifying: def foo(): print
The function I'm calling with call_user_func is supposed to return FALSE. So how do
Im experiencing a strange situation which i was supposed to handle. The situation is:
I've got bonobo git server that's supposed to handle syncing among at least 2
Unchecked exceptions are alright if you want to handle every failure the same way,
Is there a way to view stack elements like un-assigned return values or exceptions
I'm trying to catch an error generated by flask-sqlalchemy. My code unfortunately can't handle
Consider the case where a function foo(MyClass* mc) is supposed to retain a copy
Suppose I have a function that throws an exception. Suppose this function is called
Hey there code warriors and stack exchangers. I have been trying in vain to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.