如何检查python sqlite3中是否存在记录并在tkinter中引发错误?

乘风 python 209

原文标题How to check if record is exist in python sqlite3 and raise error in tkinter?

嗨,我是 sql 新手,我正在尝试使用 sqlite3 数据库后端创建一个 tkinter 应用程序。我想向 sqlite 数据库插入数据,如果记录存在,它会在 tkinter 应用程序中引发错误。

原文链接:https://stackoverflow.com//questions/71507759/how-to-check-if-record-is-exist-in-python-sqlite3-and-raise-error-in-tkinter

回复

我来回复
  • P S Solanki的头像
    P S Solanki 评论

    只需进行 SQL 查询以查看记录是否已存在。

    如果是,则显示带有错误消息的消息框。

    检查记录的存在可以通过

    SELECT EXISTS(SELECT 1 FROM table_name WHERE some_column="some_value");
    

    然后在你的代码中,你可以做

    import tkinter.messagesbox as msgb
    if exists:  # exists value comes from executing the query above
        msgb.showerror('Record already exists', 'Looks like the record alreadye xists')
    else:
        # continue to inset the record
        pass
    
    2年前 0条评论