是否可以根据条件退出当前功能但不结束程序

问题描述 投票:0回答:1

我有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()
sql python-3.x exit
1个回答
0
投票

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

test = Database.accountExistance(email, phone)
if test == False:
    return # Stops the execution of the function
© www.soinside.com 2019 - 2024. All rights reserved.