Page 1 of 1

MYSQLDUMP replace or ignore duplicate

Posted: Thu 19 Jan 2012 11:34
by Denker
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

Posted: Wed 25 Jan 2012 12:09
by Pinturiccio
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; }
        }

tank you

Posted: Thu 02 Feb 2012 15:06
by Denker
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