I want to globally replace all instances of my site url in the WordPress MySQL database with a new site url. To do that, I’m using phpMyAdmin to export the database to a .sql file, then doing a global replace in a text editor, then using phpMyAdmin to import the .sql file.
During the import, I’m encountering a duplicate entry for primary key error. Trying to debug this, I exported the file, then imported the identical file, making no changes and I still get the same error.
I appreciate any help solving this.
--
-- Dumping data for table `wp_comments`
--
INSERT INTO `wp_comments`
(`comment_ID`, `comment_post_ID`, `comment_author`, `comment_author_email`
,`comment_author_url`, `comment_author_IP`, `comment_date`, `comment_date_gmt`
,`comment_content`, `comment_karma`, `comment_approved`, `comment_agent`
,`comment_type`, `comment_parent`, `user_id`)
VALUES (1, 1, 'Mr WordPress', ''
,'http://wordpress.org/', '', '2011-04-28 00:49:55', '2011-04-28 00:49:55'
,'Hi, this is a comment.<br />To delete a comment,
just log in and view the post's comments.
There you will have the option to edit or delete them.'
, 0, 'post-trashed', '', '', 0, 0 ) ;
MySQL said:
#1062 - Duplicate entry '1' for key 'PRIMARY'
The original data is still in the database.
If you were working manually you’d send a
UPDATE
rather thanINSERT
query with the new values, but since you’re doing a batch import, it may just be better to wipe the existing table clean before the import.Be sure to back-up your data, first.
To avoid duplicates you have to use UPDATE instead of INSERT statements. To achieve this in phpMyAdmin, follow these steps:
box labeled “Function to use when dumping data:” select “UPDATE”
(default is “INSERT”).
browse for the edited file. Click GO
You are ready! To check if everything is OK, search the database second time (repeat step 2). You should not find any tables containing your old string.
If you’re exporting, that means that the main content stays in the database. So, when you try to insert a new row with the same PRIMARY KEY, which are always UNIQUE, you’ll get an error.
Solution: You must delete the row from the table that has the same comment_ID.
You must open the PHPMyAdmin and go your table page, and check the row with the ID you want. In this case is 1, which means that it is probabily in the first results page of your table listing. Just click on the red X which appears in the row.
This could also be done by SQL, which could be simple too.
For this, just select your database in PHPMyAdmin and go to the SQL tab, and insert the code above in the text area.
Had the same problem and error number. Deleted the database, recreate with no tables, and import the changed export file worked for me.
The problem is related with your file – you are trying to create a DB using a copy – at the top of your file you will find something like this:
CREATE DATABASE IF NOT EXISTS
*THE_NAME_OF_YOUR_DB*
DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci;USE
*THE_NAME_OF_YOUR_DB*
;and I’m sure that you already have a DB with this name – IN THE SAME SERVER – please check, because you are trying to overwrite!! Just change the name OR (better) ERASE THIS LINE!
Since you already have that record there, you can just update the record rather than inserting. It would go something like this.
Just update every instance of your old address using this method. You can search through all of all posts by saying “WHERE ‘comment_author_url’ = ‘YOUR OLD ADDRESS'”
If all you want to do is replace your URL, I believe this is all you must do:
Just type the above SQL into PHPMyAdmin’s SQL box and execute.
NOTE: First make sure you have a backup. And there’s no need to do all that export and import stuff 🙂
Change to code to
This will fix your problem with a minimum of fuss, whilst still inserting new rows.