Insert only if date is not duplicated - Python

Asked

Viewed 40 times

0

Good afternoon, you guys! Today I am making an Internet that does not check if such record has already been entered in the database. Through the variables: name, date and number an Index is performed in the missing table:

nome = message.who_name
numero = message.who
data = date.today()

sql = "insert into faltantes values ('', %s, %s, %s)
valores = (nome, numero, data)
cursor.execute(sql, valores)

I would like to know if you can help me, so that this Internet will only be held if the student’s name is not already entered and the date does not contain the date of the day. That date of day I refer to the date.Oday function

I appreciate anyone who can help me!

  • You know the clause unique of SQL?

1 answer

1


Create a unique key:

CREATE TABLE faltantes (
   UNIQUE KEY(nome,data) 
);
  • Thank you Marcos Grzeça, I used your comment + Insert ignore and managed to register and is not + duplicating.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.