i’m taking data from textfile which contains some duplicate data.And i’m trying to insert them into database without duplicating.i’m in trouble where inserting duplicate data.it should not be inserted again.data are not static values.
text_file = open(min_file, “r”) #doc = text_file.readlines() for line in text_file: field = line.split(“;”) print(field)
try: connection = mysql.connector.connect(host='localhost', database='testing', user='root', password='root') if connection.is_connected(): db_Info = connection.get_server_info() print("Connected to MySQL Server version ", db_Info) cursor = connection.cursor() cursor.execute("select database();") record = cursor.fetchone() print("You're connected to database: ", record) mycursor = connection.cursor() #before inserting mycursor.execute("Select * from ftp") myresult = mycursor.fetchall() for i in myresult: print(i) sql ="Insert into ftp(a,b,c,d) select * from( Select VALUES(%s,%s,%s,%s) as temp where not exists (Select a from ftp where a = %s) LIMIT 1" mycursor.execute(sql,field) print(mycursor.rowcount, "record inserted.") connection.commit() except Error as e: print("Error while connecting to MySQL", e) finally: if connection.is_connected(): cursor.close() connection.close() print("MySQL connection is closed")
One option is to add Unique constraint and let the DB validate uniqueness, this will throw exception which you can catch and skip.