How to ignore a "Duplicate entry error" in MYSQL and insert the remaining un-duplicated

Asked

Viewed 82 times

0

How to ignore a exception and insert the rest of the fields when I get this error in MYSQL using mysql . NET Connector? Mine try catch is as follows:

try
    {
        conexao.Open();
        MySqlDataReader reader = comm.ExecuteReader();
        reader.Read();
        return reader.GetString("ID");

    }
    catch (MySqlException e)
    {
        if (e.Number == 1062) //Duplicate entry
        {
            MySqlDataReader reader = comm.ExecuteReader();
            reader.Read();
            return reader.GetString("ID");
        }
        else
        {
            Debug.WriteLine(Query);
            Debug.WriteLine(e);
            return "Error";
        }

    }
    catch (Exception e)
    {
        Debug.WriteLine(Query);
        Debug.WriteLine(e);
        return "Error";
    }
finally
{

        conexao.Close(); //Fechando a conexão
}

Of course when you have duplicate emails inside values of my query I fell into the first catch (MySqlException e) inside it I began to filter the error by if (e.Number == 1062 he checks and falls right into that if, but when I will execute the command MySqlDataReader reader = comm.ExecuteReader(); MYSQL warns again of the error and code goes to the finally and let’s say if I have 1 duplicated email out of 100 emails, I would like to insert 99 and ignore the email that already exists in the table.

  • Thought to make a select by checking if the email already exists?

  • It’s a lot of emails, it’s an INSERT with N-type email numbers

  • calmai, are you using 1 Insert to add multiple emails? Your bank is normalized?

  • @Gabrielcoletta yes, what happens is I analyze a folder of an email (in case the company’s email) and look for emails sent with error to clear my other email base, not to be ordering the bank all the time I give a INSERT on the table EMAILS_ERROS with several emails together(INSERT INTO table VALUES(),(),()

  • And it’s very well normalized.

  • I never used INSERT like this and I didn’t even know I had.

  • https://dev.mysql.com/doc/refman/5.5/en/insert.html take a look at

Show 2 more comments
No answers

Browser other questions tagged

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