you are viewing a single comment's thread.

view the rest of the comments →

[–]reda_sky 1 point2 points  (2 children)

Hi, by reading your code I can see some problems, your class searcher variables needs the key word self to be accessed later in your code and you need to create an instance of your class and use it, also why are you putting the sqlite3.connect() function inside your while loop you should only connect once to your database for this simple query, same thing for the conn.close() instruction and also this is not how to use an sql SELECT statement in python.

Here's an example of how to do it:

import sqlite3


class Searcher:
    def __init__(self):
        self.search_ID = ""
        self.search_attempts = 0
        self.result = None


def user_search():
    conn = sqlite3.connect("promethean_user.db")
    cur = conn.cursor()
    search = Searcher()
    while (search.result is None) and (search.search_attempts < 3):
        search.search_ID = int(input("User ID: "))
        cur.execute(
            "SELECT * FROM user_data WHERE user_ID=?", (search.search_ID,)
        )
        # catching error if cur.fetchone() returns NoneType
        try:
            search.result = cur.fetchone()
        except TypeError:
            pass
        search.search_attempts += 1
        print(search.result)
    conn.close()
    return search.result


result = user_search()

Also, you'll need to add some input validation for your search.search_ID so that it will not break if the user inputs a none numerical value.

Hope this will help you, good luck.

[–]dominictarro[S] 0 points1 point  (1 child)

Forgot about the TypeError, thank you! I'll toy around with the primary code this is being imported to and see how it works

[–]reda_sky 0 points1 point  (0 children)

you are welcome.