Python Forum
[Solved]Help with SQLite Login Form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved]Help with SQLite Login Form
#1
Hello,

I'm trying to make a login form using Python and SQLite as a database. I am able to match the Name and Password to get a successful login, but I am trying to add a third variable (Privilege) which will take the user to a different menu depending on their privilege status (admin or StandardUser ).

This is what I have so far:

My CreateUserTable.py
import sqlite3

def createDatabase():
        #Create a database (users.db)
        connection = sqlite3.connect("users.db")
        cursor = connection.cursor()

        table = """CREATE TABLE IF NOT EXISTS Users
                (ID INTEGER PRIMARY KEY  AUTOINCREMENT,
                Name           TEXT    NOT NULL,
                Password       INT     NOT NULL,
                Privilege      TEXT    NOT NULL);"""

        #Execute the creation of the table
        cursor.execute(table)
        #print("The database has been created")
        #Commit the changes
        connection.commit()
        #Close the connection
        connection.close() 
What's in my Table right now:
Output:
[('Admin', 1234, 'admin')]
A snippet of my Login Code:
 import sqlite3
        connection = sqlite3.connect("users.db")
        cursor = connection.cursor()
        cursor.execute(f"SELECT Name from users WHERE Name ='{userInputName}' AND Password = '{userInputPassword}';")
        connection.commit()
        if not cursor.fetchone():  # An empty result evaluates to False.
            print("Login failed")
        else:
            print("Welcome")
        
        #Close the connection
        connection.close()
I need to grab the Privilege column from the table and use it to determine which menu it will take the user to.

If the user is an admin call AdminMenu()
else if the user is a StandardUser call MainMenu()
else call CredentialsError()

Any way I can do this?

Thanks in advance.
Reply
#2
Add privilege to your select statement then show the correct menu occordding to the returned result.
ibreeden likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Jun-11-2022, 12:48 AM)menator01 Wrote: Add privilege to your select statement then show the correct menu occordding to the returned result.

Ok, I got that part.
(This is what I have)
        #----------------------------------
        import sqlite3
        connection = sqlite3.connect("users.db")
        cursor = connection.cursor()
                
        # cursor.execute('''
        #         insert into Users (Name, Password, Privilege)
        #         values ('Admin',1234,'admins')
        #         ''')
        cursor.execute("SELECT*FROM Users")
        print(cursor.fetchall())
        
        cursor.execute(f"SELECT Privilege from Users WHERE Name ='{userInputName}' AND Password = '{userInputPassword}';")
        connection.commit()
        privilege = cursor.fetchone()
        print("Result: ",privilege)

        if privilege == "admins":
            print("Hello admin User")
        elif privilege == "user":
            print("Hello standard user")
        else:
            print("Try again")
        
        #Close the connection
        connection.close()
        #----------------------------------
This is what it outputs if I type in Name:Bob, & Password:1234:
Output:
[(1, 'Admin', 1234, 'admins'), (2, 'Admin', 1234, 'admins'), (3, 'Bob', 1234, 'user')] Result: ('user',) Try again
Why doesn't it print("Hello standard user")?

Thanks in advance.
Reply
#4
('user', ) is a tuple. A tuple will never equal a string.
Reply
#5
(Jun-24-2022, 02:17 PM)deanhystad Wrote: ('user', ) is a tuple. A tuple will never equal a string.

So how do I make it a string?
Is there a way to convert tuples to strings?
Reply
#6
Never mind.

Used str.join()

        privilege1 = cursor.fetchone()
        print("Result: ",privilege1)

        privilege = ''.join(privilege1)
Thanks for the help.
Reply
#7
You shouldn't be using f-strings to use variables in your queries (line 13 above). Instead, you should be using the driver's placeholders - see https://docs.python.org/3/library/sqlite3.html, specifically from "Instead, use the DB-API’s parameter substitution".
Reply
#8
Join is wrong. A tuple is like a list. Use indexing to get the values.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved]Help with search statement-SQLite & Python Extra 1 1,050 May-06-2022, 07:38 PM
Last Post: Extra
  in a login interface when i try login with a user supposed to say test123 but nothing NullAdmin 3 2,260 Feb-20-2021, 04:43 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020