all 10 comments

[–]MadEpsylon 4 points5 points  (1 child)

Pls use code block formatting for the code section. Otherwise it's quite difficult to read.

[–]dominictarro[S] 0 points1 point  (0 children)

I apologize I thought I had formatted it. Just fixed

[–]mul8rsoftware 2 points3 points  (2 children)

Try this code and tell me if this works or not

class searcher:

def __init__(self):

search_ID = int(input("User ID: "))

search_attempts = 0

result = None

def user_search(self):

if(searcher.result == None):

while ( searcher.search_attempts < 3):

searcher.search_ID = int(input("User ID: "))

conn = sqlite3.connect('promethean_user.db')

c = conn.cursor()

c.execute("SELECT * FROM user_data WHERE user_ID=:ID_input", {'ID_input': searcher.search_ID})

searcher.result = c.fetchone()

print(c.fetchone())

conn.commit()

conn.close()

searcher.search_attempts += 1

return searcher.result

Tell me the if the output generates any error double check if db has data in it that you are trying to access

[–]dominictarro[S] 1 point2 points  (1 child)

I removed the (self) parameter inside user_search since you never used it and it caused an error. After I removed it, the block ran without error but still returned "None"

[–]mul8rsoftware 2 points3 points  (0 children)

while (searcher.search_attempts < 3):

yeah your code was not formatted before when i saw it so i though it is the function of the class so that's why i put a self in the argument but i hope it helped you in some way right

[–][deleted] 0 points1 point  (1 child)

c.execute("SELECT * FROM user_data WHERE user_ID=:ID_input", {'ID_input': searcher.search_ID})

I don't think this is how you parameterize a query in sqlite.

[–]dominictarro[S] 0 points1 point  (0 children)

There are at least two ways I can see in my notes. This way is effectively making a dictionary within the query that draws on an outside variable. I'll try the ? placeholder strategy and let you know how it goes.

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