Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #Libraries
  2. import sqlite3
  3.  
  4.  
  5.  
  6. #Variables
  7.  
  8. #Connect to the database.
  9. conn = sqlite3.connect('database.db')
  10. c = conn.cursor()
  11.  
  12.  
  13. #Authentication
  14.  
  15. def login():
  16. #Do this when you've logged in.
  17. def loggedIn(userid):
  18. def viewPlaylist(userid):
  19. print(userid)
  20. c.execute("SELECT * FROM playlists where userid=?", (userid))
  21.  
  22. if(c.fetchone):
  23. print(c.fetchone)
  24. else:
  25. printError("ERROR: No Data found, with that UserID\nCreating Data...")
  26. c.execute("INSERT INTO playlists VALUES(NULL,?,NULL)", (userid))
  27. printError("Finished!\nYou may now edit your playlist.")
  28. loggedIn(userid)
  29.  
  30. def editPlaylist():
  31. print()
  32.  
  33. print("""
  34. Welcome !
  35. Select one of the following:
  36.  
  37. 1. My Playlist
  38. 2. Edit Playlist
  39.  
  40. 3. Exit\n
  41. """)
  42.  
  43. choice = input(" > ")
  44.  
  45. if choice == "1":
  46. viewPlaylist(userid)
  47. elif choice == "2":
  48. editPlaylist(userid)
  49. else:
  50. printError("ERROR\nThat is no valid choice.")
  51. loggedIn()
  52.  
  53. ########################SPACING############################
  54.  
  55.  
  56. username= input("Username: ").lower()
  57. password= input("Password: ")
  58.  
  59. #Check if there account exists
  60.  
  61.  
  62. c.execute("SELECT 1 FROM accounts WHERE username=? AND password=?", (username, password))
  63.  
  64.  
  65. if(c.fetchone()):
  66. #Goto the account page and give the parameter ID so you can load information from that user.
  67. loggedIn(c.execute("SELECT id FROM accounts where username=?",(username, )).fetchone()
  68. )
  69. else:
  70. printError("That user doesn't exist.")
  71.  
  72. def register():
  73. username= input("Username: ").lower()
  74. password= input("Password: ")
  75.  
  76.  
  77. #Get username and see if it exists
  78. c.execute("SELECT id FROM accounts WHERE username=?",(username, )).fetchone()
  79.  
  80. if(c.fetchone()):
  81. printError("ERROR: Username is already In use!")
  82. register()
  83. else:
  84. #Insert values into the account table in the database.
  85. c.execute("INSERT INTO accounts VALUES(NULL,?,?)", (username,password))
  86.  
  87. conn.commit()
  88.  
  89. #Grab ID
  90. ids = c.execute("SELECT id FROM accounts WHERE username=?", (username, )).fetchone()
  91.  
  92. #Insert defaults into playlists
  93. c.execute("INSERT INTO playlists VALUES(NULL,?,NULL)", (ids))
  94.  
  95. #Save the changes.
  96. conn.commit()
  97.  
  98. login(loggedIn(c.execute("SELECT id FROM accounts where username=?",(username)).fetchone()))
  99. #Menu
  100. def menu():
  101. print("""
  102. Welcome!
  103. To Begin please select one of the following:
  104.  
  105. 1. Login
  106. 2. Register
  107.  
  108. """)
  109.  
  110. #Get users choice
  111. choice = input(" > ")
  112.  
  113. #Check if it's valid and if so then do something.
  114. if choice == "1":
  115. login()
  116. elif choice == "2":
  117. register()
  118.  
  119. else:
  120. printError("ERROR\nThat is no valid choice.")
  121. menu()
  122.  
  123. #Error function (Not needed but helps to see which are errors and prints)
  124. def printError(error):
  125. print(error)
  126.  
  127. #Start with the menu
  128. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement