Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.18 KB | None | 0 0
  1. import mysql.connector
  2. cnx = mysql.connector.connect(host="localhost",user="root", password="newpass?21")
  3. #print(cnx)
  4. mycursor = cnx.cursor()
  5. mycursor.execute("CREATE TABLE students (name VARCHAR(255), subject VARCHAR(255), grade INTEGER(100))")
  6.  
  7. def main():
  8.     cnx = mysql.connector.connect(host="localhost",user="root", password="newpass?21")
  9.     # h metavliti cnx.cursor() den yparxei pou8ena
  10.     # einai cnx.cursor()
  11.     # mycursor = cnx.cursor()
  12.     mycursor = cnx.cursor()
  13.     # Auto thn deuterh fora pou trexei kwdika den xreiazetai
  14.     # Giati ka8e fora pou trexei to programma 8a ftiaxnei thn vash
  15.     #mycursor.execute("CREATE TABLE students (name VARCHAR(255), subject VARCHAR(255), grade INTEGER(100))")
  16.  
  17.     message = ("The valid actions are:" )
  18.     message2 = ("insert // read // delete // update // view // exit ")
  19.  
  20.     print(message)
  21.     print(message2)
  22.     userInput = ""
  23.     while userInput != "exit":
  24.         userInput = raw_input("enter what you want to do:")
  25.         if userInput == "insert":
  26.             createEntry()
  27.         elif userInput == "read":
  28.             retrieveEntry()
  29.         elif userInput == "delete":
  30.             deleteEntry()
  31.         elif userInput == "update":
  32.             updateEntry()
  33.         elif userInput == 'view':
  34.             viewEntries()
  35.  
  36. def viewEntries():
  37.     mycursor = cnx.cursor()
  38.     mycursor.execute("SELECT * FROM students")
  39.     myresult = mycursor.fetchall()
  40.     for row in myresult:
  41.         print(row)
  42.     main()
  43.     pass
  44.  
  45.  
  46.  
  47. def createEntry():
  48.     mycursor = cnx.cursor()
  49.     print("Give a name:")
  50.     namei = input()
  51.     print("Give a subject:")
  52.     subjecti = input()
  53.     print("Give a grade:")
  54.     gradei = input()
  55.     sqlFormula = "INSERT INTO students (name, subject, grade) VALUES (%s, %s, %s)"
  56.     val = (namei, subjecti, gradei)
  57.     mycursor.execute(sqlFormula, val)
  58.  
  59.     cnx.commit()
  60.  
  61.     print(mycursor.rowcount, "record inserted.")
  62.     main()
  63.     "We will use this function to create DB Entries"
  64.     pass
  65.  
  66. def retrieveEntry():
  67.     print("Retrieve entries by name, by subject, by grade. y/n?")
  68.     answer = input()
  69.     if answer == "y":
  70.         print("By name = n, by subject = s, by grade = g:")
  71.         entriesby = input()
  72.         if entriesby == "n":
  73.             print("Give a name:")
  74.             namer = input()
  75.             mycursor = cnx.cursor()
  76.             mycursor.execute("SELECT * FROM students WHERE name = 'namer'")
  77.             myresultbyname = mycursor.fetchall()
  78.             for result in myresultbyname:
  79.                 print(result)
  80.             main()
  81.         elif entriesby == "s":
  82.             print("Give a subject:")
  83.             subjectr = input()
  84.             mycursor = cnx.cursor()
  85.             mycursor.execute("SELECT * FROM students WHERE subject = 'subjectr'")
  86.             myresultbysubject = mycursor.fetchall()
  87.             for result in myresultbysubject:
  88.                 print(result)
  89.             main()
  90.         elif entriesby == "g":
  91.             print("Give a grade:")
  92.             grader = input()
  93.             mycursor = cnx.cursor()
  94.             mycursor.execute("SELECT * FROM students WHERE grade = 'grader'")
  95.             myresultbygrade = mycursor.fetchall()
  96.             for result in myresultbygrade:
  97.                 print(result)
  98.             main()
  99.     elif answer =="n":
  100.         print("Retrieve entries by starting character/s? y/n")
  101.         answer2 = input()
  102.         if answer2 == "y":
  103.             print("Starting character/s of name = n, subject = s, grade = g")
  104.             startchar = input()
  105.             if starchar == 'n':
  106.                 print("Give the starting character/s:")
  107.                 starcharn = input()
  108.                 mycursor = cnx.cursor()
  109.                 mycursor.execute("SELECT * FROM students WHERE name LIKE 'starcharn%'")
  110.                 myresultbynamec = mycursor.fetchall()
  111.                 for result in myresultbynamec:
  112.                     print(result)
  113.                 main()
  114.             elif starchar == 's':
  115.                 print("Give the starting character/s:")
  116.                 starchars = input()
  117.                 mycursor = cnx.cursor()
  118.                 mycursor.execute("SELECT * FROM students WHERE subject LIKE 'starchars%'")
  119.                 myresultbysubjectc = mycursor.fetchall()
  120.                 for result in myresultbysubjectc:
  121.                     print(result)
  122.                 main()
  123.             elif starchar == 'g':
  124.                 print("Give the starting character/s:")
  125.                 starcharg = input()
  126.                 mycursor = cnx.cursor()
  127.                 mycursor.execute("SELECT * FROM students WHERE grade LIKE 'starcharg%'")
  128.                 myresultbygradec = mycursor.fetchall()
  129.                 for result in myresultbygradec:
  130.                     print(result)
  131.                 main()
  132.         elif answer2 == "n":
  133.             print ("Retrieve entries by last character/s? y/n")
  134.             answer3 = input()
  135.             if answer3 == 'y':
  136.                 print ("By name = n or subject = s?")
  137.                 answer4 = input()
  138.                 if answer4 == 'n':
  139.                     print ("Give the last character/s:")
  140.                     lastcharn = input()
  141.                     mycursor = cnx.cursor()
  142.                     mycursor.execute("SELECT * FROM students WHERE name LIKE '%lastcharn'")
  143.                     myresultbynamecl = mycursor.fetchall()
  144.                     for result in myresultbynamecl:
  145.                         print(result)
  146.                     main()
  147.                 elif answer4 == 's':
  148.                     print ("Give the last character/s:")
  149.                     lastchars = input()
  150.                     mycursor = cnx.cursor()
  151.                     mycursor.execute("SELECT * FROM students WHERE name LIKE '%lastchars'")
  152.                     myresultbysubjectcl = mycursor.fetchall()
  153.                     for result in myresultbysubjectcl:
  154.                         print(result)
  155.                     main()
  156.             elif answer3 == 'n':
  157.                 main()
  158.  
  159.  
  160. def updateEntry():
  161.     mycursor = cnx.cursor()
  162.     print  ('Update by name = n, subject = s, grade = g:')
  163.     optionu = input()
  164.     if optionu == "n":
  165.         nameu = input("Give a name:")
  166.         print ("Update a subject = s or a grade = g")
  167.         optionn = input()
  168.         if optionn == "s":
  169.             print  ("Give a subject:")
  170.             subjectn = input()
  171.             sql = "UPDATE students SET subject = 'subjectn' WHERE name = 'nameu' "
  172.             mycursor.execute(sql)
  173.             cnx.commit()
  174.             main()
  175.         elif optionn == "g":
  176.             print  ("Give a grade:")
  177.             graden = input()
  178.             sql = "UPDATE students SET grade = 'graden' WHERE name = 'nameu'"
  179.             mycursor.execute(sql)
  180.             cnx.commit()
  181.             main()
  182.     elif optionu == "s":
  183.         subjectu = input("Give a subject:")
  184.         print  ("Update a name = n or a grade = g")
  185.         options = input()
  186.         if options == "n":
  187.             print ("Give a name:")
  188.             names = input()
  189.             sql = "UPDATE students SET name = 'names' WHERE subject = 'subjectu'"
  190.             mycursor.execute(sql)
  191.             cnx.commit()
  192.             main()
  193.         elif options == "g":
  194.             print ("Give a grade:")
  195.             grades = input()
  196.             sql = "UPDATE students SET grade = 'grades' WHERE subject = 'subjectu'"
  197.             mycursor.execute(sql)
  198.             cnx.commit()
  199.             main()
  200.     elif optionu == "g":
  201.         gradeu = input("Give a grade:")
  202.         print("Update a name = n or a subject = s")
  203.         optiong = input()
  204.         if optiong == "n":
  205.             print("Give a name:")
  206.             nameg = input()
  207.             sql = "UPDATE students SET name = 'nameg' WHERE grade = 'gradeu'"
  208.             mycursor.execute(sql)
  209.             cnx.commit()
  210.             main()
  211.         elif optiong == "s":
  212.             print("Give a subject:")
  213.             subjectg = input()
  214.             sql = "UPDATE students SET subject = 'subjectg' WHERE grade = 'gradeu' "
  215.             mycursor.execute(sql)
  216.             cnx.commit()
  217.             main()
  218.  
  219. def deleteEntry():
  220.     mycursor = cnx.cursor()
  221.     print('Choose to delete by name = n,subject = s,grade = g:')
  222.     optiond = input()
  223.     if optiond == "n":
  224.         print("Give a name:")
  225.         nameD = input()
  226.         sql = "DELETE FROM students WHERE name = 'nameD'"
  227.         mycursor.execute(sql)
  228.         cnx.commit()
  229.         main()
  230.  
  231.     elif optiond == 's':
  232.         print("Give a subject:")
  233.         subjectD = input()
  234.         sql = "DELETE FROM students WHERE subject = 'subjectD'"
  235.         mycursor.execute(sql)
  236.         cnx.commit()
  237.         main()
  238.  
  239.     elif optiond == 'g':
  240.         print("Give a grade:")
  241.         gradeD = input()
  242.         sql = "DELETE FROM students WHERE grade = 'gradeD'"
  243.         mycursor.execute(sql)
  244.         cnx.commit()
  245.         main()
  246.  
  247. # The reason why we use
  248. # if __name__== "__main__":
  249. # is
  250. # if we want to reuse this code
  251. # and import it somewhere as library
  252. # we don't want the code after
  253. # if __name__== "__main__":
  254. # to be executed
  255. # Now that we don't use this code as a library
  256. # we want to run the main() function
  257. # as soon as the programm start
  258. if __name__== "__main__":
  259.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement