Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Libraries
- import sqlite3
- #Variables
- #Connect to the database.
- conn = sqlite3.connect('database.db')
- c = conn.cursor()
- #Authentication
- def login():
- #Do this when you've logged in.
- def loggedIn(userid):
- def viewPlaylist(userid):
- print(userid)
- c.execute("SELECT * FROM playlists where userid=?", (userid))
- if(c.fetchone):
- print(c.fetchone)
- else:
- printError("ERROR: No Data found, with that UserID\nCreating Data...")
- c.execute("INSERT INTO playlists VALUES(NULL,?,NULL)", (userid))
- printError("Finished!\nYou may now edit your playlist.")
- loggedIn(userid)
- def editPlaylist():
- print()
- print("""
- Welcome !
- Select one of the following:
- 1. My Playlist
- 2. Edit Playlist
- 3. Exit\n
- """)
- choice = input(" > ")
- if choice == "1":
- viewPlaylist(userid)
- elif choice == "2":
- editPlaylist(userid)
- else:
- printError("ERROR\nThat is no valid choice.")
- loggedIn()
- ########################SPACING############################
- username= input("Username: ").lower()
- password= input("Password: ")
- #Check if there account exists
- c.execute("SELECT 1 FROM accounts WHERE username=? AND password=?", (username, password))
- if(c.fetchone()):
- #Goto the account page and give the parameter ID so you can load information from that user.
- loggedIn(c.execute("SELECT id FROM accounts where username=?",(username, )).fetchone()
- )
- else:
- printError("That user doesn't exist.")
- def register():
- username= input("Username: ").lower()
- password= input("Password: ")
- #Get username and see if it exists
- c.execute("SELECT id FROM accounts WHERE username=?",(username, )).fetchone()
- if(c.fetchone()):
- printError("ERROR: Username is already In use!")
- register()
- else:
- #Insert values into the account table in the database.
- c.execute("INSERT INTO accounts VALUES(NULL,?,?)", (username,password))
- conn.commit()
- #Grab ID
- ids = c.execute("SELECT id FROM accounts WHERE username=?", (username, )).fetchone()
- #Insert defaults into playlists
- c.execute("INSERT INTO playlists VALUES(NULL,?,NULL)", (ids))
- #Save the changes.
- conn.commit()
- login(loggedIn(c.execute("SELECT id FROM accounts where username=?",(username)).fetchone()))
- #Menu
- def menu():
- print("""
- Welcome!
- To Begin please select one of the following:
- 1. Login
- 2. Register
- """)
- #Get users choice
- choice = input(" > ")
- #Check if it's valid and if so then do something.
- if choice == "1":
- login()
- elif choice == "2":
- register()
- else:
- printError("ERROR\nThat is no valid choice.")
- menu()
- #Error function (Not needed but helps to see which are errors and prints)
- def printError(error):
- print(error)
- #Start with the menu
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement