Advertisement
Guest User

final.py

a guest
Feb 6th, 2022
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.27 KB | None | 0 0
  1. import pandas as pd
  2. import mysql.connector
  3. con = mysql.connector.connect( host ="localhost",user= "u0_a359",passwd= "pass" ,database = "mukesh" )
  4. df1 = pd.read_sql("select * from covid;",con)
  5. print (df1)
  6.  
  7. def add():
  8.       print("")
  9.       a=input("\t Enter the state:   ")
  10.       print("")
  11.       b=int (input("\t Enter the cases: "))
  12.       print("")
  13.       c=int (input("\t Enter the death: "))
  14.       print("")
  15.       d=int (input("\t Enter the population:  "))
  16.       print("")
  17.       e=int (input("\t Enter the vaccinated_dose1:  "))
  18.       print("")
  19.       f=int (input("\t Enter the vaccinated_dose2:  "))
  20.       q1 = "insert into covid values ( '%s',%d,%d,%d,%d,%s );"%(a,b,c,d,e,f)
  21.       con.cursor().execute(q1)
  22.       con.commit()
  23.       print("")
  24.       print("Record added successfully")
  25.       df2 = pd.read_sql("select * from covid;",con)
  26.       print ("")
  27.       print (df2)
  28.    
  29. def search ():
  30.       print("")
  31.       print ("\t search record from following options: \n \t 1.state. \n \t 2.cases. \n \t 3.death. \n \t 4.population \n\t 5.vaccinated_dose1: \n\t 6.vaccinated_dose2: ")
  32.       x=int(input("enter the option: "))
  33.       if  x==1:
  34.         a= input("enter state name: ")
  35.         q2="select * from  covid where state='%s';" %a
  36.         df3= pd.read_sql ( q2, con )
  37.         print(df3)
  38.       elif x==2:
  39.           b= int (input("enter cases: "))
  40.           q3="select * from  covid  where cases=%d;" %b
  41.           df4= pd.read_sql ( q3, con )
  42.           print(df4)
  43.       elif x==3:
  44.           c = int ( input (" enter death: "))
  45.           q4="select * from covid  where death=%d;" %c
  46.           df5 = pd.read_sql ( q4, con )    
  47.           print(df5)
  48.       elif x==4:
  49.           d= int(input("enter population: "))
  50.           q5="select * from covid where population='%d';" %d
  51.           df6= pd.read_sql ( q5, con )    
  52.           print(df6)
  53.       elif x==5:
  54.           e = int ( input (" enter vaccinated_dose1: "))
  55.           q6="select * from covid where vaccinated_dose1='%d';" %e
  56.           df7 = pd.read_sql ( q6, con )    
  57.           print(df7)
  58.       elif x==6:
  59.           f = int ( input (" enter vaccinated_dose2:  "))
  60.           q7="select * from covid where vaccinated_dose2='%d';" %f
  61.           df8 = pd.read_sql ( q7, con )    
  62.           print(df8)
  63.       else:
  64.           print ("")
  65.           print ("invalid input!")
  66.          
  67. def update ():
  68.       print ("Update record from following options: \n \t 1.state.  \n \t 2.cases. \n\t 3.death.  \n\t 4.population. \n\t 5.vaccinated_dose1. \n\t 6.vaccinated_dose2. ")
  69.       y = int ( input (" enter choice:  "))
  70.       if y==1:
  71.           df1 = pd.read_sql("select * from covid;",con)
  72.           print ("before update")
  73.           print (df1)
  74.           a=input("enter existing record: ")
  75.           b=input("enter new record: ")
  76.           q1="update covid set state = '%s' where state = '%s';" %(b,a)
  77.           con.cursor().execute(q1)
  78.           con.commit()
  79.           print (" Record updated successfully ")
  80.           df2= pd.read_sql("select * from covid;",con)
  81.           print ("after update")
  82.           print ( df2 )
  83.       elif y==2:
  84.           df1 = pd.read_sql("select * from covid;",con)
  85.           print ("before update")
  86.           print (df1)
  87.           a=int (input("enter existing record: "))
  88.           b=int (input("enter new record: "))
  89.           q2="update covid set cases = %d where cases = %d;" %(b,a)
  90.           con.cursor().execute(q2)
  91.           con.commit()
  92.           print (" Record updated successfully ")
  93.           df2= pd.read_sql("select * from covid;",con)
  94.           print ("after update")
  95.           print ( df2 )
  96.       elif y==3:
  97.           df1 = pd.read_sql("select * from covid;",con)
  98.           print ("before update")
  99.           print (df1)
  100.           c=int (input("enter existing record: "))
  101.           d=int (input("enter new record: "))
  102.           q2="update covid set death = %d where death = %d;" %(d,c)
  103.           con.cursor().execute(q2)
  104.           con.commit()
  105.           print (" Record updated successfully ")
  106.           df2= pd.read_sql("select * from covid;",con)
  107.           print ("after update")
  108.           print ( df2 )
  109.       elif y==4:
  110.           df1 = pd.read_sql("select * from covid;",con)
  111.           print ("before update")
  112.           print (df1)
  113.           c=int (input("enter existing record: "))
  114.           d=int (input("enter new record: "))
  115.           q2="update covid set population = %d where population = %d;" %(d,c)
  116.           con.cursor().execute(q2)
  117.           con.commit()
  118.           print (" Record updated successfully ")
  119.           df2= pd.read_sql("select * from covid;",con)
  120.           print ("after update")
  121.           print ( df2 )
  122.       elif y==5:
  123.           df1 = pd.read_sql("select * from covid;",con)
  124.           print ("before update")
  125.           print (df1)
  126.           c=int (input("enter existing record: "))
  127.           d=int (input("enter new record: "))
  128.           q2="update covid set vaccinated_dose1= %d where vaccinated_dose1= %d;" %(d,c)
  129.           con.cursor().execute(q2)
  130.           con.commit()
  131.           print (" Record updated successfully ")
  132.           df2= pd.read_sql("select * from covid;",con)
  133.           print ("after update")
  134.           print ( df2 )
  135.       elif y==6:
  136.           df1 = pd.read_sql("select * from covid;",con)
  137.           print ("before update")
  138.           print (df1)
  139.           c=int (input("enter existing record: "))
  140.           d=int (input("enter new record: "))
  141.           q2="update covid set vaccinated_dose2= %d where vaccinated_dose2= %d;" %(d,c)
  142.           con.cursor().execute(q2)
  143.           con.commit()
  144.           print (" Record updated successfully ")
  145.           df2= pd.read_sql("select * from covid;",con)
  146.           print ("after update")
  147.           print ( df2 )
  148.          
  149. def delete():
  150.       print("")
  151.       print ("Delete record from following options: \n \t 1.state. \n \t 2.cases. \n\t 3.death.  \n\t 4.population. \n\t 5.vaccinated_dose1. \n\t 6.vaccinated_dose2.  ")
  152.       print("")
  153.       y = int ( input ("enter choice:  "))
  154.       if y==1:
  155.           df1 = pd.read_sql("select * from covids;",con)
  156.           print ("before delete")
  157.           print (df1)
  158.           c= input("enter record you want to delete: ")
  159.           q1="DELETE from  covid where state ='%s';" %c
  160.           con.cursor().execute(q1)
  161.           con.commit()
  162.           print (" Record deleted successfully ")
  163.           df2= pd.read_sql("select * from covid;",con)
  164.           print ("after delete")
  165.           print ( df2)
  166.       elif y==2:
  167.           df1 = pd.read_sql("select * from covid;",con)
  168.           print ("before delete")
  169.           print (df1)
  170.           c=int( input("enter record you want to delete: "))
  171.           q1="DELETE from  covid where cases =%d;" %c
  172.           con.cursor().execute(q1)
  173.           con.commit()
  174.           print (" Record deleted successfully !")
  175.           df2= pd.read_sql("select * from covid;",con)
  176.           print ("after delete")
  177.           print ( df2)
  178.       if y==3:
  179.           df1 = pd.read_sql("select * from covid;",con)
  180.           print ("before delete")
  181.           print (df1)
  182.           c=int( input("enter record you want to delete :  "))
  183.           q1="DELETE from  covid where death=%d;" %c
  184.           con.cursor().execute(q1)
  185.           con.commit()
  186.           print (" Record deleted successfully !")
  187.           df2= pd.read_sql("select * from covid;",con)
  188.           print ("after delete")
  189.           print ( df2)
  190.       if y==4:
  191.           df1 = pd.read_sql("select * from covid;",con)
  192.           print ("before delete")
  193.           print (df1)
  194.           c=int( input("enter record you want to delete :  "))
  195.           q1="DELETE from  covid where population=%d;" %c
  196.           con.cursor().execute(q1)
  197.           con.commit()
  198.           print (" Record deleted successfully !")
  199.           df2= pd.read_sql("select * from covid;",con)
  200.           print ("after delete")
  201.           print ( df2)
  202.       if y==5:
  203.           df1 = pd.read_sql("select * from covid;",con)
  204.           print ("before delete")
  205.           print (df1)
  206.           c=int( input("enter record you want to delete :  "))
  207.           q1="DELETE from  covid where vaccinated_dose1=%d;" %c
  208.           con.cursor().execute(q1)
  209.           con.commit()
  210.           print (" Record deleted successfully !")
  211.           df2= pd.read_sql("select * from covid;",con)
  212.           print ("after delete")
  213.           print ( df2)
  214.       if y==6:
  215.           df1 = pd.read_sql("select * from covid;",con)
  216.           print ("before delete")
  217.           print (df1)
  218.           c=int( input("enter record you want to delete :  "))
  219.           q1="DELETE from  covid where vaccinated_dose2=%d;" %c
  220.           con.cursor().execute(q1)
  221.           con.commit()
  222.           print (" Record deleted successfully !")
  223.           df2= pd.read_sql("select * from covid;",con)
  224.           print ("after delete")
  225.           print ( df2)
  226.          
  227. def menu():
  228.       print("\t\t MENU  \n \t 1. show all records.\n \t 2. add records.\n \t 3. search records.\n \t 4. delete record. \n \t 5. update record. \n \t 6. exit.")
  229.       print ("")
  230.       x = int( input( "  enter choice number( integer only):  "))
  231.      
  232.       if  x==1:
  233.           print (df1)
  234.           print ("")
  235.           print ("  To go back to menu press 'y' else 'n'   ")
  236.           print ("")
  237.           y = input ("  back to menu:  ")
  238.           if  y=='y':
  239.               print("")
  240.               menu()
  241.           elif  y=='n':
  242.               print("")
  243.               print ( "  Thanks for using my program 🙏🙏🙂")
  244.           else:
  245.               print("")
  246.               print("  invalid input!  ")
  247.               print ("")
  248.               menu()
  249.              
  250.       elif x ==2:
  251.           add()
  252.           print ("")
  253.           print ("  To go back to menu press 'y' else 'n'   ")
  254.           print ("")
  255.           y = input ("  back to menu:  ")
  256.           if  y=='y':
  257.               print("")
  258.               menu()
  259.           elif  y=='n':
  260.               print("")
  261.               print ( "  Thanks for using my program 🙏🙏🙂 ")
  262.           else:
  263.               print("")
  264.               print("  invalid input!  ")  
  265.               print("")
  266.               menu()
  267.              
  268.       elif x ==3:
  269.           search()
  270.           print ("")
  271.           print ("  To go back to menu press 'y' else 'n'   ")
  272.           print ("")
  273.           y = input ("  back to menu:  ")
  274.           if  y=='y':
  275.               print("")
  276.               menu()
  277.           elif  y=='n':
  278.               print("")
  279.               print ( "  Thanks for using my program 🙏🙏🙂")
  280.           else:
  281.               print("")
  282.               print("  invalid input!   ")
  283.               print ("")
  284.               menu()
  285.              
  286.       elif x==4:
  287.         delete()
  288.         print ("")
  289.         print ("  To go back to menu press 'y' else 'n'   ")
  290.         print ("")
  291.         y = input ("  back to menu:  ")
  292.         if  y=='y':
  293.             print("")
  294.             menu()
  295.         elif  y=='n':
  296.             print("")
  297.             print ( "  Thanks for using my program 🙏🙏🙂")
  298.         else:
  299.             print("")
  300.             print("  invalid input!   ")
  301.             print ("")
  302.             menu()
  303.            
  304.       elif x==5:
  305.         update ()
  306.         print ("")
  307.         print ("  To go back to menu press 'y' else 'n'   ")
  308.         print ("")
  309.         y = input ("  back to menu:  ")
  310.         if  y=='y':
  311.             print("")
  312.             menu()
  313.         elif  y=='n':
  314.             print("")
  315.             print ( "  Thanks for using my program 🙏🙏🙂")
  316.         else:
  317.             print("")
  318.             print("  invalid input!   ")
  319.             print ("")
  320.             menu() 
  321.            
  322.       elif x==6:
  323.         print ("")
  324.         y = input ( "  To EXIT the program press 'y' else 'n':   ")
  325.         if  y=='y':
  326.             print("")
  327.             print ( "  Thanks for using my program 🙏🙏🙂")
  328.         elif  y=='n':
  329.             print("")
  330.             menu ()
  331.         else:
  332.             print("")
  333.             print("  invalid input!   ")
  334.             print ("")
  335.             menu()         
  336.       else:
  337.           print("")
  338.           print ("  invalid input!  " )  
  339.           print("")
  340.           menu()       
  341. con.cursor().execute(menu())
  342.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement