'm尝试在mySQL中创建员工数据库,在其中用户必须从userChoice中进行选择。如果UserChoice是1,则应通过插入方法进行插入方法,如果用户将不正确的数据类型用于循环,则必须进行3次尝试。如果他选择2,则应通过用户可以在表中选择要删除的字段的删除。更新与删除方法相同。UserChoice应运行,直到用户将其指定为e。如果用户指定错误的输入,则程序应返回错误。编辑代码将是我的新手。感谢您的帮助:(
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
首先,当您拥有choices=input("Enter your choice:")
时,您无法声明字符串e
,请使用raw_input()
。正如Python文档所说,使用RAW_INPUTS作为用户的一般输入。
第二:代码的格式错误。如果要在类中的while
,则应在def userchoice()
中标记。
第三:对于这个问题。将insert(), delete(), update()
实现从While循环中取出。
第四:在__main__
函数中,您不应该像属性一样访问方法。从类访问方法时,您应该将()
添加到方法名称(如有必要时,请传递参数(。
您的代码应该看起来像这样:
class MyEmploye(object):
def create(self, *args, **kwargs):
(...)
def delete(self, *args, **kwargs):
(...)
def update(self, *args, **kwargs):
(...)
def insert(self, *args, **kwargs):
(...)
def userchoice(self, *args, **kwargs):
print ("n Enter 1 to insert datasn" +
" Enter 2 to...n " +
" Enter 3 to.. n" +
" Enter 4 to... n")
while choices != 'e':
choices = raw_input("enter your choice: ")
if choices == '1':
self.insert()
elif choices == '2':
self.delete()
elif choices == '3':
self.update()
elif choices == '4':
self.create()
if __name__=='__main__':
az=MyEmploye()
az.userchoice()