Advertisement
Guest User

Untitled

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