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
MYSQLDUMP replace or ignore duplicate
-
- Devart Team
- Posts: 2420
- Joined: Wed 02 Nov 2011 09:44
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
Thank you, but i have another fix for the Problem.
I only replace "INSERT INTO" to "INSERT IGNORE INTO" and Dump.restore works without any error.
Thanks
Denker
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)
Thanks
Denker