jeudi 20 avril 2017

python mysql codes to create employee database with user choice and inputs

I'm trying to create employee database in MySql where the user has to select from userchoice. if the userchoice is 1, then it should go through insert method where the for loop has to run for 3 tries if the user gives incorrect data type in for loop. If he selects 2 it should go through delete where the user can select which field in the table to delete. Update is same as that of delete method. The userchoice should run until the user specifies it as e. The program should return an error if the user specifies wrong input. Editing the code would be preferred as I'm a newbie. Thanks for your help :)

Here are my codes:

class Emp():

    def create():

        abc= "CREATE TABLE tablename ( NAME CHAR(20), IDNO INT PRIMARY KEY, AGE INT,POSITION CHAR(20), SALARY INT)"
        cursor.execute(abc)

    def userchoice():
        print ("\n Enter 1 to insert datas")
        print ("\n Enter 2 to delete datas")
        print ("\n Enter 3 to update datas")
        print ("\n Enter e to exit")
        choices=input("Enter your choice:")

while choices !=e:

    def insert():
        if choices==1:
            try:
                tries=1
                n=int(raw_input("No of Empl:"))
                while tries<3:

                    for i in range(n):
                        name =raw_input("enter the name:")
                        mail_id =raw_input("enter the mail id:")
                        age =int(raw_input("enter the age:"))
                        salary =int(raw_input("enter salary:"))

                    print "Datas entered successfully"
                    cursor.execute(insert)
                    db.commit()
                    break
            except ValueError:
                print " max tries over"
                tries=tries+1

    def delete():
        if choices==2:
            print ("\n Enter 1 to delete name")
            print ("\n Enter 2 to delete position")
            print ("\n Enter 3 to delete age")
            print ("\n Enter 4 to delete salary")
            choe=input("Enter your choice:")
            try:
                if choe==1:
                    "ALTER TABLE tablename DROP 'name'";

                if choe==2:
                    "ALTER TABLE tablename DROP 'position'";

                if choe==3:
                    "ALTER TABLE tablename DROP 'age'";

                if choe==4:
                    "ALTER TABLE tablename DROP 'salary'";
                else:
                    print "select the correct choice to delete"
                cursor.execute(delete)
                db.commit()
            except ValueError:
                 print "enter crct i/p"

    def update():

        if choices==3:
            print ("\n Enter 1 to update name")
            print ("\n Enter 2 to update position")
            print ("\n Enter 3 to update age")
            print ("\n Enter 4 to update salary")

            cho=raw_input("Enter your choice:")

            if cho==1:
                "UPDATE employee_db SET Name = Name";

            if cho==2:
                "UPDATE employee_db SET Name";

            if cho==3:
                "UPDATE employee_db SET age = age + 5";

            if cho==4:
                "UPDATE employee_db SET SALARY = SALARY + 5000";
            else:
                print "enter the crct choice to update"

        if choices==4:
            print "bye"
while userchoice =='e':
    break
print "Please select the correct choice"

if __name__=='__main__':
    az=Emp()
    az.userchoice
    az.insert
    az.delete
    az.update






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire