Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1. import sqlite3 as lite
  2. import sys
  3. import datetime
  4. from datetime import datetime
  5.  
  6. con = lite.connect('test.db')
  7. #Title Screen asking for Login, Register or Exit
  8. while True :
  9. TitleScreen = raw_input("Login, Register or Exit? ")
  10. #Login Module
  11. if TitleScreen in ["Login", "login"] :
  12. while True:
  13. with con:
  14. cur = con.cursor()
  15. cur.execute("CREATE TABLE IF NOT EXISTS Users(user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL)")
  16. LoginAns = raw_input('Login to existing user?')
  17. if LoginAns in ['Yes', 'y', 'yes', 'Y', "YES"]:
  18. LoginUser = raw_input('Username: ')
  19. LoginPassword = raw_input('Password: ')
  20. params = (LoginUser, LoginPassword)
  21. cur.execute("SELECT count(*) FROM Users WHERE username = ?", (LoginUser,))
  22. dataLogin = cur.fetchone()[0]
  23. if dataLogin == 1:
  24. cur.execute("SELECT count(*) FROM Users WHERE password =?", (LoginPassword,))
  25. dataPass = cur.fetchone()[0]
  26. if dataPass == 1:
  27. print('Successful login')
  28. #Timetable Creation
  29. cur.execute("SELECT user_id FROM Users WHERE username = ? ", (LoginUser,))
  30.  
  31. UserTuple = cur.fetchone()
  32. CurrentUserID = UserTuple[0]
  33.  
  34. while True:
  35. cur.execute("CREATE TABLE IF NOT EXISTS Events(event_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, timetable_id INTEGER, user_id INTEGER NOT NULL, event_name TEXT NOT NULL, event_descript TEXT, location_id INTEGER, datetime_id INTEGER)")
  36.  
  37. cur.execute("CREATE TABLE IF NOT EXISTS location(location_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, timetable_id INTEGER, user_id INTEGER NOT NULL, location_name TEXT NOT NULL)")
  38.  
  39. cur.execute("CREATE TABLE IF NOT EXISTS DateTable(datetime_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, timetable_id INTEGER, user_id INTEGER NOT NULL, EventDateTime DATE NOT NULL)")
  40.  
  41.  
  42. TimetableAction = raw_input('Add new event, view existing event, delete existing event or exit?')
  43. if TimetableAction in ['Add new event', 'ADD NEW EVENT', 'add new event', 'add', 'new', 'new event', 'Add']:
  44.  
  45. NewEventName = raw_input('Name of event: ')
  46. NewEventDescript = raw_input('Description of event: ')
  47. NewEventLocation = raw_input("Location of event: ")
  48.  
  49. NewEventDateTime = str(raw_input('Input Year, Month, Day, Hour, Minute (Format = "y, m, d, H, M") '))
  50. try:
  51. dt_start = datetime.strptime(NewEventDateTime, '%Y, %m, %d, %H, %M')
  52. except ValueError:
  53. print "Incorrect Format"
  54.  
  55. params = (CurrentUserID, NewEventLocation)
  56. cur.execute("INSERT INTO location VALUES(NULL, NULL, ?, ?)", params)
  57. LocationID = cur.lastrowid
  58.  
  59. params = (CurrentUserID, dt_start)
  60. cur.execute("INSERT INTO DateTable VALUES(NULL, NULL, ?, ?)", params)
  61. DateTimeID = cur.lastrowid
  62.  
  63. params = (CurrentUserID, NewEventName, NewEventDescript, LocationID, DateTimeID)
  64. cur.execute("INSERT INTO Events VALUES(NULL, NULL, ?, ?, ?, ?, ?)", params)
  65.  
  66. elif TimetableAction in ['View existing event', 'View', 'view', 'existing', 'Existing', 'event', 'Event', 'VIEW EXISTING EVENT', 'view existing event']:
  67.  
  68. SearchBy = raw_input('Search by Name or Date?')
  69. if SearchBy in ['Name', 'name', 'NAME']:
  70.  
  71. SearchName = raw_input('Input Name ')
  72.  
  73. cur.execute("SELECT event_name FROM Events WHERE event_name =?", (SearchName,))
  74. EventNameTuple = cur.fetchone()
  75. SearchedEventName = EventNameTuple[0]
  76.  
  77. print SearchedEventName
  78.  
  79. cur.execute("SELECT event_descript FROM Events WHERE event_name =?", (SearchName,))
  80. EventDescriptTuple = cur.fetchone()
  81. SearchedEventDescript = EventDescriptTuple[0]
  82.  
  83. print SearchedEventDescript
  84.  
  85. cur.execute("SELECT location_id FROM Events WHERE event_name =?", (SearchName,))
  86. LocationIDValueTuple = cur.fetchone()
  87. SearchedLocationID = LocationIDValueTuple[0]
  88.  
  89. cur.execute("SELECT location_name FROM location WHERE location_id =?", (SearchedLocationID,))
  90. LocationNameTuple = cur.fetchone()
  91. SearchedLocationName = LocationNameTuple[0]
  92.  
  93. print SearchedLocationName
  94.  
  95. cur.execute("SELECT datetime_id FROM Events WHERE event_name =?", (SearchName,))
  96. DateTimeIDValueTuple = cur.fetchone()
  97. SearchedDateTimeID = DateTimeIDValueTuple[0]
  98.  
  99. cur.execute("SELECT EventDateTime FROM DateTable WHERE datetime_id =?", (SearchedDateTimeID,))
  100. DateTimeTuple = cur.fetchone()
  101. SearchedDateTime = DateTimeTuple[0]
  102.  
  103. print SearchedDateTime
  104. #PROBLEM
  105. if SearchBy in ['Date', 'date', 'DATE']:
  106.  
  107. SearchDate = str(raw_input('Input Year, Month, Day, Hour, Minute (Format = "y, m, d, H, M") '))
  108. try:
  109. dt_end = datetime.strptime(SearchDate, '%Y, %m, %d, %H, %M')
  110. except ValueError:
  111. print "Incorrect Format"
  112.  
  113. cur.execute("SELECT datetime_id FROM DateTable where EventDateTime =?", (dt_end,))
  114.  
  115. print(cur.fetchone())
  116.  
  117. DateTimeID2Value = cur.fetchone()
  118. SearchedDateTimeID2 = DateTimeID2Value[0]
  119.  
  120. print SearchedDateTimeID2
  121.  
  122. elif TimetableAction in ["Delete", "delete", "delete existing", "delete existing event", "Delete existing event", "Delete existing"]:
  123.  
  124. DeleteEventName = raw_input('Input name of event to be delete ')
  125.  
  126. cur.execute("Select location_id FROM Events WHERE event_name =?", (DeleteEventName,))
  127. DeleteEventLocationIDTuple = cur.fetchone()
  128. DeleteEventLocationID = DeleteEventLocationIDTuple[0]
  129.  
  130. cur.execute("Select datetime_id FROM Events WHERE event_name =?", (DeleteEventName,))
  131. DeleteEventDateTimeIDTuple = cur.fetchone()
  132. DeleteEventDateTimeID = DeleteEventDateTimeIDTuple[0]
  133.  
  134. cur.execute("Delete FROM Events WHERE event_name =?", (DeleteEventName,))
  135. cur.execute("Delete FROM location WHERE location_id =?", (DeleteEventLocationID,))
  136. cur.execute("Delete FROM DateTable WHERE datetime_id =?", (DeleteEventDateTimeID,))
  137.  
  138. print "Successfully deleted"
  139.  
  140. elif TimeTableAction in ["exit", "Exit", "EXIT"]:
  141. break
  142. else:
  143. print('Wrong password')
  144. else:
  145. print('No such user exists')
  146. elif LoginAns in ['No', 'no', 'N', 'n', "NO"]:
  147. break
  148. else:
  149. print "Invalid answer"
  150. #Register Module
  151. elif TitleScreen in ["Register", "register"] :
  152. with con:
  153.  
  154. cur = con.cursor()
  155. cur.execute("CREATE TABLE IF NOT EXISTS Users(user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL)")
  156. while True:
  157. RegisterAns = raw_input('Create new User? ')
  158. if RegisterAns in ["Yes", "Y", "yes", "y", "YES"]:
  159. Newusername = raw_input('Create Username: ')
  160. password = raw_input('Create password: ')
  161. params = (Newusername, password)
  162. cur.execute("SELECT count(*) FROM Users WHERE username = ?", (Newusername,))
  163. dataRegister = cur.fetchone()[0]
  164. if dataRegister == 1:
  165. print('User already exists')
  166. else:
  167. cur.execute("INSERT INTO Users VALUES(NULL, ?, ?)", params)
  168. print('Successfully registered')
  169. elif RegisterAns in ["No", "N", "n", "no", "NO"]:
  170. break
  171. else:
  172. print "Invalid input"
  173. #Exit module
  174. elif TitleScreen in ["Exit", "exit"] :
  175. sys.exit()
  176. else :
  177. print "Invalid input"
  178.  
  179. cur.execute("CREATE TABLE Events(event_id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL UNIQUE, event_name TEXT NOT NULL, event_descript TEXT, location_id INTEGER, time_id INTEGER, date_id INTEGER)")
  180.  
  181. cur.execute("CREATE TABLE location(location_id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL UNIQUE, location_name TEXT NOT NULL, location_descript TEXT, )")
  182.  
  183. cur.execute("CREATE TABLE timetable(timetable_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER NOT NULL UNIQUE, event_name TEXT NOT NULL, date_id INTEGER, time_id INTEGER)")
  184.  
  185. cur.execute("CREATE TABLE DateTable(date_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER NOT NULL UNIQUE, EventDate DATE NOT NULL)")
  186.  
  187. cur.execute("CREATE TABLE time(time_id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL UNIQUE, time datetime NOT NULL, event_id INT NOT NULL, date_id INT NOT NULL)")
  188.  
  189. B = raw_input("user ID")
  190. C = raw_input("event name")
  191. D = raw_input("event description")
  192. E = raw_input("location id")
  193. F = raw_input("time id")
  194. G = raw_input("date id")
  195.  
  196.  
  197. params = (B, C, D, E, F, G)
  198.  
  199.  
  200. cur.execute("INSERT INTO Events VALUES(NULL, ?, ?, ?, ?, ?, ?)", params)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement