Sqlite3.IntegrityError : échec de la contrainte UNIQUE
Solved
Janken
-
yg_be Posted messages 23437 Registration date Status Contributor Last intervention -
yg_be Posted messages 23437 Registration date Status Contributor Last intervention -
Hello.
I am encountering a small problem with my database. When I try to make an entry in my email table, which has a column for the identifier (id) and a column for the email, the following error is displayed: sqlite3.IntegrityError: UNIQUE constraint failed: emails.id.
Here is my little program in Python:
Thank you for your help.
I am encountering a small problem with my database. When I try to make an entry in my email table, which has a column for the identifier (id) and a column for the email, the following error is displayed: sqlite3.IntegrityError: UNIQUE constraint failed: emails.id.
Here is my little program in Python:
from tkinter import * from random import * from string import * from random import * from sqlite3 import * # Database to save email addresses connection = connect('base.db') # Cursor for queries to the database cursor = connection.cursor() # Window window = Tk() # Function to set a random background color def hexadecimal(): bits = list(str(i) for i in range(10)) + list("ABCDEF") colorcode = "#" for i in range(6): colorcode = colorcode + choice(bits) return colorcode # Window characteristics window.title("Window") window.minsize(720, 480) window.config(background=hexadecimal()) # Frame frame = Frame(window, bg=window['background']) # Function to generate a random email address def generate_email(): entry.delete(0, END) chars = ascii_lowercase + digits + "." email = sample(chars, randint(5, 10)) + list("@gmail.com") for i in email: email = "".join(email) entry.insert(0, email) cursor.execute("INSERT INTO emails values(?, ?)", (cursor.lastrowid, email)) connection.commit() # Widgets entry = Entry(frame, bg=hexadecimal(), bd=2) button = Button(frame, text="Click", background=hexadecimal(), bg=hexadecimal(), command=generate_email) # Display the elements frame.pack(expand=YES) entry.pack() button.pack() # Display the window window.mainloop() # Close the database connection connection.close() Thank you for your help.
1 answer
yg_be
Posted messages
23437
Registration date
Status
Contributor
Last intervention
Ambassadeur
1 588
Hello,
on which line of code do you get this error?
have you checked the value of
how did you define the table emails?
on which line of code do you get this error?
have you checked the value of
cursor.lastrowid?
how did you define the table emails?
The error occurs at line 41. Since I've set my table's identifier to auto-increment, the value of cursor.lastrowid shouldn't logically cause any issues.
In fact, when I run the program and click once on the button that triggers the email registration function, no error occurs, but when I click more than once, the error happens.
If you examine the value of each time, you will understand what is happening.