MYSQLDUMP replace or ignore duplicate

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
Denker
Posts: 6
Joined: Thu 19 Jan 2012 10:32

MYSQLDUMP replace or ignore duplicate

Post by Denker » Thu 19 Jan 2012 11:34

Hi,

I Want to backup only new Data from MYSQLDUMP, it works but sometimes old Rows are in the Dump, then i got an error, with duplicate keys.
Can i restore a MYSQLDUMP and ignore or replace duplicate Keys?


Thanks
Denker

Pinturiccio
Devart Team
Posts: 2192
Joined: Wed 02 Nov 2011 09:44

Post by Pinturiccio » Wed 25 Jan 2012 12:09

We do not have options for ignoring duplicate keys. As a workaround, you can use the MySqlDump.Error event and ignore all 'Duplicate entry' exceptions:

Code: Select all

        static void Main(string[] args)
        {
            MySqlConnection conn = new MySqlConnection("host=db;user id=root;password=root;port=3309;");
            conn.Database = "test";
            conn.Open();
            MySqlDump dump = new MySqlDump(conn);
            dump.Mode = Devart.Common.DumpMode.Data;
            dump.Tables = "Dept;Emp";
            StreamWriter sw = new StreamWriter(@"D:\TMP\ignore.txt");
            dump.Backup();
            dump.Error+=new ScriptErrorEventHandler(dump_error);
            sw.WriteLine(dump.DumpText);
            sw.WriteLine("INSERT INTO Dept VALUES (70, 'ACCOUNTING', 'NEW YORK');");
            sw.Close();
            dump.Restore(@"D:\TMP\ignore.txt");
            conn.Close();
        }

        private static void dump_error(object sender, ScriptErrorEventArgs e)
        {
            if (e.Exception.Message.IndexOf("Duplicate entry") != -1) { e.Ignore = true; }
        }

Denker
Posts: 6
Joined: Thu 19 Jan 2012 10:32

tank you

Post by Denker » Thu 02 Feb 2012 15:06

Thank you, but i have another fix for the Problem.

Code: Select all

dump.UseExtSyntax = True
dump.backup(datei)
sr = New System.IO.StreamReader(datei)
       neu = sr.ReadToEnd
            sr.Close()
            neu = Replace(neu, "INSERT INTO", "INSERT IGNORE INTO")
            sw = New System.IO.StreamWriter(datei)
            sw.Write(neu)
       sw.Close()
dump.restore(datei)
I only replace "INSERT INTO" to "INSERT IGNORE INTO" and Dump.restore works without any error.

Thanks
Denker

Post Reply