MYSQLDUMP replace or ignore duplicate

MYSQLDUMP replace or ignore duplicate

Postby 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
Denker
 
Posts: 6
Joined: Thu 19 Jan 2012 10:32

Postby 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; }
        }
Pinturiccio
Devart Team
 
Posts: 2021
Joined: Wed 02 Nov 2011 09:44

tank you

Postby 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
Denker
 
Posts: 6
Joined: Thu 19 Jan 2012 10:32


Return to dotConnect for MySQL