【问题标题】:Error: #1062 - Duplicate entry '1' for key 'PRIMARY'错误:#1062 - 键 'PRIMARY' 的重复条目 '1'
【发布时间】:2014-02-02 11:17:26
【问题描述】:

我已获得要上传到我的服务器的数据库文件,但在导入时收到以下错误消息。我已经搜索过其他类似的问题,但不知道如何解决这个问题。

SQL 查询:

--
-- 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/',
     '', 
     '0000-00-00 00:00:00',
     '0000-00-00 00:00:00',
     'Hi, this is a comment.<br />To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.',
     0,
     '1', 
     '', 
     '', 
     0,
     0 
    );

MySQL 说:

#1062 - 键“PRIMARY”的重复条目“1”

【问题讨论】:

标签: mysql sql


【解决方案1】:

wp_cmets 表中的comment_ID 列设置为PRIMARY KEY,因此不允许将具有相同comment_ID 值的多条记录插入到表中。

在您的情况下,您已经插入了一条值为“1”的记录,因此您要么必须手动删除该记录(或清空整个表),要么从不同的 comment_ID 开始插入,或者干脆省略 comment_ID 列和值,并且很可能会自动填充。

重新格式化您的查询,如下所示:

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 ( null, 1, 'Mr WordPress', '', 'http:////wordpress.org//', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', "Hi, this is a comment. 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, '1', '', '', 0, 0 ) ;

comment_ID 的值为 null,或

INSERT INTO wp_comments ( 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, 'Mr WordPress', '', 'http:////wordpress.org//', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', "Hi, this is a comment. 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, '1', '', '', 0, 0 ) ;

comment_ID 及其值被完全省略。

在这两种情况下,MySQL 都会自动分配下一个自动递增的值。

【讨论】:

  • 您好,我已尝试两种方式重新格式化,但仍然不成功。顶部重新格式化,收到此消息:#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法以使用 near 的 cmets。在那里你可以选择编辑或删除它们。', 0, '1', '',' 在第 6 行
  • 您的查询中有一个撇号。要么删除它,要么用双引号替换整个字符串周围的单引号。
  • 对不起,听起来像个新手,但下落在这里... VALUES (null, 1, 'Mr WordPress', '', 'wordpress.org', '', '0000-00- 00 00:00:00', '0000-00-00 00:00:00', '嗨,这是一条评论。要删除评论,只需登录并查看帖子的 cmets。在那里您可以选择编辑或删除它们。', 0, '1', '', '', 0, 0 ) ;
  • 没问题,我已经在答案中编辑了更正的解决方案。立即尝试。
  • SQL 查询: -- -- 转储表 wp_comments 的数据 -- 插入 wp_cmets(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 ( NULL , 1, 'Mr WordPress', '', 'wordpress.org/'; MySQL 说:#1064 - 你的 SQL 语法有错误;查看对应的手册您的 MySQL 服务器版本,以便在第 6 行的 '' 附近使用正确的语法
【解决方案2】:

您是否尝试迁移 WordPress 网站?

如果是这样,如果你做了以下,就会出现上面提到的错误。

  1. 您已备份 WordPress MySQL 数据库
  2. 您设置了一个新的 WordPress 网站
  3. 然后您尝试在此新文件中恢复备份 设置 WordPress 网站

在步骤 2 之后,您应该首先从新的 WordPress 数据库中删除所有表,您将在其中恢复步骤 3 中的备份。

出现错误消息是因为您尝试将 MySQL 转储(数据库文件)导入非空 WordPress 数据库。当您设置 WordPress 站点时,它会创建所需的表,并插入一些示例记录,例如评论。因此,您的数据库中已经有一条 id==1 的评论,而您的备份尝试添加另一个 id==1 的评论(很可能是您网站上添加的第一条真正的评论)。

【讨论】:

    【解决方案3】:

    您可能没有 AUTO_INCREMENT 主键。可以在这里找到一个例子:

    http://dev.mysql.com/doc/refman/5.1/de/example-auto-increment.html

    CREATE TABLE animals (
        id MEDIUMINT NOT NULL AUTO_INCREMENT,
        name CHAR(30) NOT NULL,
        PRIMARY KEY (id)
    );
    

    【讨论】:

    • 它是一个 wordpress 表格,并且已经对初选进行了自动增量。
    • 哦 wordpress ...好吧,忘记我写的内容。
    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    相关资源
    最近更新 更多