【问题标题】:Incorrect password message box密码错误消息框
【发布时间】:2022-01-24 07:06:19
【问题描述】:
import mysql.connector
from tkinter import messagebox

from mysql.connector import Error, InternalError

logman = mysql.connector.connect(host = 'localhost',user = 'root', passwd = '12345678', database = 'basictest')

logwife = logman.cursor(buffered=True)

def wxy(use,pas):
    logboy = ('SELECT Username, Password FROM patient WHERE Password =' + str(pas))

    logwife.execute(logboy)

    x = list(logwife.fetchone())

    print(x)

    if x[0] == use:
        messagebox.showinfo('success','You have successfully Logged in')

    else:
        messagebox.showerror('Failed','Wrong Credentials')

我想这样做,以便在密码错误时显示一个消息框。但是我抛出一个错误

【问题讨论】:

  • 错误是什么?

标签: python tkinter


【解决方案1】:

最终的SQL语句无效,例如pas的值为"pass",那么最终的SQL语句为:

SELECT Username, Password FROM patient WHERE Password = pass

最好使用占位符

还要检查是否返回记录,否则x = list(logwife.fetchone())会抛出异常。

以下是更新后的wxy()

def wxy(use, pas):
    # use placeholder
    logboy = 'SELECT Username, Password FROM patient WHERE Password = %s'
    logwife.execute(logboy, (str(pas),)) # 2nd argument must be tuple or list
    x = logwife.fetchone()
    print(x)

    # better check whether x is None or not before using it
    if x is not None and x[0] == use:
        messagebox.showinfo('success', 'You have successfully Logged in')
    else:
        messagebox.showerror('Failed', 'Wrong Credentials')

我建议在 SQL 中检查用户名和密码:

def wxy(use, pas):
    logboy = 'SELECT 1 FROM patient WHERE Username = %s and Password = %s'
    logwife.execute(logboy, (use, pas))
    x = logwife.fetchone()
    print(x)
    if x is not None:
        messagebox.showinfo('success','You have successfully Logged in')
    else:
        messagebox.showerror('Failed','Wrong Credentials')

请注意,出于安全考虑,不建议将纯文本密码存储在数据库中。

【讨论】:

    【解决方案2】:

    这些代码行给出了你想要的错误。

    else 可能有问题

    from tkinter import messagebox
    
    messagebox.showerror("Title", "Message")
    

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 2014-09-24
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多