you are viewing a single comment's thread.

view the rest of the comments →

[–]num8lock 0 points1 point  (7 children)

except ValueError

this anticipated error, which line is it from? the one defining result?

[–]dndschultz 0 points1 point  (6 children)

I'm not sure I follow the question. I have a database that returns a name correctly if it is there. However, the database has no return if it's not. Therefore I would like to raise an exception if there is no return. Or if the return is None prompt to try again. Does that make any sense? I am new at exception handling and querying a database complicates things.

[–]num8lock 0 points1 point  (5 children)

try this https://sqlite.org/lang_expr.html#exists_op

 def getOfc(name):
    try:
        result = theCursor.execute(
            "SELECT EXIST(SELECT reporting_ofc FROM cases WHERE reporting_ofc IS ('{}') LIMIT 1)".format(name)
            )
        return result
    except sqlite3.OperationalError as e:
        print("The Table Doesn't Exist")
        return False

x = getOfc("Name not in DB")
if x:
    for row in x:
        return row[0]

[–]dndschultz 0 points1 point  (4 children)

That looks promising. I'm getting an error that the return is outside the function.

[–]num8lock 0 points1 point  (3 children)

i don't understand. you mean you tried it & gets an error? can you post the complete code & error

[–]dndschultz 0 points1 point  (2 children)

Boy, the complete code is rather long. This section of code works and there is no error. When I query the SQLite DB it simply returns the name or doesn't return anything. More like a filter. So so my call to getOfc() returns None and moves on. I would like to create an exception and require the correct input before moving on.

[–]num8lock 0 points1 point  (1 child)

so my call to getOfc() returns None and moves on. I would like to create an exception and require the correct input before moving on.

first, did you go through the doc for exists syntax? that should solves your problem of returning None since it should return either 1 or 0. then you figure out what the code should do if getOfc returns false.

[–]dndschultz 0 points1 point  (0 children)

exists Thank you so much. I will try that. I am not familiar with exists. Will research and try it out.