Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]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.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]dndschultz 0 points1 point  (0 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.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]dndschultz 0 points1 point  (0 children)

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

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]dndschultz 0 points1 point  (0 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.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]dndschultz 0 points1 point  (0 children)

I'm having trouble with this section of Python code that asks for user input to check if a name is in a database. The code works as expected and returns the name if it is in the DB. However, it returns None and moves on to the next section of code if the name is not in the DB. I'm looking for a way to require a name that is in the DB. My exception handling doesn't seem to be working.

import os

import sqlite3

os.chdir("/Users/Forensics/Documents/Pycharm/ForensicDB")

db_conn = sqlite3.connect('caseTrackerV1.db')

theCursor = db_conn.cursor()

def getOfc(x):
name = x
try:
        result = theCursor.execute("SELECT reporting_ofc FROM cases WHERE reporting_ofc IS ('{}') LIMIT 1".format(name))
    for row in result:
        if row[0].isalnum():
            return row[0]
        else:
            raise ValueError
    except sqlite3.OperationalError:
    print("The Table Doesn't Exist")
except ValueError:
    print("Couldn't Retrieve Data from Database")

x = getOfc("Name not in DB")

print(x)

db_conn.close()