Home > Blockchain >  How can I make to make a complex query for my login system?
How can I make to make a complex query for my login system?

Time:03-02

I want a login system which relates to a register database I made

I am trying to make a complex query which takes the user input:

    Entry(self.root,
          textvariable=self.username)  ##There is more but I want this to be as minimal as possible 
          
    Entry(self.root,
          textvariable=self.password,
          show="*")
             

This user input then gets compared with the one in the database. This is where I am finding it diffucult:

def login(self):
    con = sqlite3.connect("register.db")  ##The database which I want to open and compare user inputs to 
    c = con.cursor()
    c.execute("SELECT * FROM register")
    slist = c.fetchall()
    values = [row[0] for row in slist]
    values2 = [row[1] for row in slist]
    if self.username.get() == values and self.password.get()==values2:
        command=self.invcon  ##A external thing I want to open if the user enters the data in correctly
    else:
        messagebox.showerror("Error","Error"parent=self.root)
    con.commit()
    con.close()

The error which is now happening is instead of opening the new window it moves into the else and pops up with the error box. Database

CodePudding user response:

The SQL "SELECT username * FROM register" should be "SELECT * FROM register".

Also values and values2 are list, so the comparison between a string (self.username.get() or self.password.get()) and a list will always be False.

However, you don't need to select all records from the table, just select the record with the username and password is enough:

def login(self):
    con = sqlite3.connect("register.db")  ##The database which I want to open and compare user inputs to 
    c = con.cursor()
    # assume the fields required are 'username' and 'password'
    # change them to suit your table definition
    c.execute("SELECT 1 FROM register WHERE username = ? AND password = ?", (self.username.get(), self.password.get()))
    result = c.fetchone() # get the record if any
    if result:
        # record found
        command=self.invcon  ##A external thing I want to open if the user enters the data in correctly
    else:
        # record not found
        messagebox.showerror("Error", parent=self.root)
    con.close()

CodePudding user response:

I don't understand all of the errors but when selecting something from a table (in this case 'register') you can either select things by listing them up like:

c.execute("SELECT username, password ... FROM register")

or you simply select everything:

c.execute("SELECT * FROM register")

In this case you did both ("SELECT username * FROM ...") which is why there could be an error

I hope this helped a bit

I wish you good luck

Regards Javid

  • Related