有没有一种方法可以根据条件退出当前功能但不结束程序

时间:2019-10-28 13:30:31

标签: sql python-3.x exit

我有2个类方法。一种方法“ accountExistance()”通过在数据库中查找重复的用户名来检查帐户是否已存在,另一种方法“ customerDetails()”将用户注册详细信息写入数据库。我的customerDetails()方法调用'accountExistance()方法,如果用户名重复(在'accountExistance'方法中进行检查),则返回False,如果返回值是False,则返回初始方法'sys.exit( )'发生。

问题在于,因为我宁愿在运行tkinter gui时也不要退出该程序,而是允许用户更改注册详细信息。而是有一种替代退出的方法,我可以取消执行所在的位置并且不向数据库提交重复的值。

请注意,在用户按下注册后,将从另一个文件中调用customerDetails(),其中输入框中的stringVars()被馈送到class方法中。此外,缩进外观在此处的格式不正确,但是我为逻辑编写了一个最小的示例,并剪切了一些代码行。

class Database():
def __init__(self):
    Database.phoneNumber = ""

def accountExistance(email, phoneNumber):
    conn=sqlite3.connect("system.db")
    cur=conn.cursor()
    emailExist = cur.execute("SELECT count(email) FROM customerDetails WHERE email = ?", (email,)).fetchall()
    conn.commit()
    print(emailExist)
    phoneExist = cur.execute("SELECT count(phone) FROM customerDetails WHERE phone = ?", (phoneNumber,)).fetchall()
    print(phoneExist)
    conn.commit()
    if emailExist or phoneExist != 0 :
        tk.messagebox.showinfo("Error", "This email and/or phone number is associated with an account")
        return False
        sys.exit()
    #else:
        #return True


#INSERT SIGN UP DETAILS
def customerDetails(forename, surname, dob, address, city,
    county, postcode, phone, email, password,verified, gender):

    print(forename, surname, dob, address, city, county, postcode,
          postcode, phone, email, password, verified, gender)

    test = Database.accountExistance(email, phone)
    if test == False:
        sys.exit()

    age = 0
    conn=sqlite3.connect("system.db")
    cur=conn.cursor()

    cur.execute("INSERT INTO customerDetails VALUES 
    (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)",(forename, surname, dob, address, city, 
    county, postcode, phone, email, password, verified, age, gender))
    conn.commit()
    conn.close()

1 个答案:

答案 0 :(得分:0)

只需将您的条件更改为以下内容:

lodash

此外,如果test = Database.accountExistance(email, phone) if test == False: return # Stops the execution of the function 必须是testTrue,则可以直接检查其值:

False

相同
if not test: