亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Sqlite Python中檢查重復數據?

如何在Sqlite Python中檢查重復數據?

慕田峪9158850 2022-08-16 10:15:39
我有這個代碼,可以從tkinter python中的輸入字段獲取電子郵件并將其存儲在數據庫中,我后來計劃使用此數據自動發送電子郵件到數據庫中的所有電子郵件。但唯一的問題是,如果我輸入相同的電子郵件兩次,它會將它們保存在數據庫中而不會出現錯誤,這將導致將重復的信息發送到電子郵件。那么任何人都可以就我應該采取什么方法提出建議嗎?def submit():# Creates the connection from the database.pyconn = sqlite3.connect("email.db")c = conn.cursor()# Insert into the database tablec.execute(    "INSERT INTO email VALUES (:email_address)", {"email_address": user_email.get()})conn.commit()conn.close()# Clear The Text Boxesuser_email.delete(0, END)這就是我從數據庫中檢索信息的方式,如果它有任何用處def emailGet():# Creates the connection from the database.pyconn = sqlite3.connect("email.db")c = conn.cursor()c.execute("SELECT *, oid FROM email")records = c.fetchall()print("This is all the emails in the database : " + str(records))global get_recordsglobal new_recordget_records = ""for i in records:    get_records += str(i[0] + ",")    #print(get_records)    new_record = get_records[:-1]    print(new_record)conn.commit()conn.close()
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

您可以使用語句來檢查從條目中獲取的電子郵件是否在數據庫中。像這樣:SELECT


def checkEmailExistance():

    email_exists = False

    c.execute('SELECT * FROM email WHERE <email adresses column> = :email_adress'

    {"email_address": user_email.get()})


    # Previous line selects a row of your database only if the column that contains

    # the email adress in said row has the same adress as the one from the entry


    if c.fetchone() # which is the same as saying "if c.fetchone retrieved something in it"

        email_exists = True 

    else:

        email_exists = False

    return email_exists

此外,我不必使用變量,而只需返回 True 或 False 來縮短代碼,我以這種方式編寫它以提高可讀性。email_exists


編輯這與您的問題無關,但是在使用數據庫時,我發現在每個自動加載和更新的表的開頭添加一個“value_id”列非常有用,您永遠不知道何時需要它!


查看完整回答
反對 回復 2022-08-16
  • 1 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號