帝国CMS后台添加信息报错Duplicate entry ‘xx’ for key ‘PRIMARY’的解决办法

帝国CMS后台添加信息报错Duplicate entry ‘xx’ for key ‘PRIMARY’,出现以下代码:

Duplicate entry
‘3261’
for key
‘PRIMARY’insert into ***_ecms_news_index(classid,checked,newstime,truetime,lastdotime,havehtml) values(
‘1’,
‘1’,
‘1446087639’,
‘1446087687’,
‘1446087687’,
‘1’);

 

这种帝国CMS报错是因为ecms_news_index索引数字不对,索引ID“3261”的信息已经存在,后添加的信息索引ID必须大于“3261”才行。

照成这种错误一般是后台丢失数据,导致索引无法正常递增混乱。

方法1:后台修复数据库

如果进的了后台尝试后台修复数据库,点击 后台 系统 备份与恢复数据 备份数据

拉到最下面 点击修复数据表和优化数据表即可

方法2:插入一个大于当前索引的信息

如果后台修复没有用,那我们就来手动或SQL插入一个大于“3261”等等信息,让索引ID重新递增。

手动操作直接参考数据库的信息,ID填一个大于“3261”的即可。

SQL插入看下面代码:

INSERT INTO `phome_ecms_news` VALUES (
3262,
1,
1,
,
,
,
1,
‘admin’,
,
1,
0,
1333244472,
0,
1,
0,
0,
‘,b|’,
,
‘1’,
0,
0,
0,
0,
0,
0,
‘企业11111’,
1333244427,
,
0,
1,
1350716513,
0,
0,
0,
0,
,
‘企业理念:诚信、专业、高效 星兴财务rn’,
0,
‘1’,
,
0,
,
0);

 

第一个字段“3262”就是索引ID,后面的参考自己的字段调整。

方法3:批量重新生成索引

如果以上都不行,只能用SQL想办法让索引ID重新生成一遍,建议分条执行,一是避免超时,二是能发现错误

CREATE TABLE [!db.pre!]ecms_newstemp AS(SELECT id,classid,newstime,truetime,lastdotime,havehtml FROM [!db.pre!]ecms_news);

ALTER TABLE `[!db.pre!]ecms_newstemp` ADD COLUMN `checked` tinyint(
1) not null DEFAULT
0 AFTER `classid`;

ALTER TABLE `[!db.pre!]ecms_newstemp` add primary key (id);

alter table [!db.pre!]ecms_news_index rename to [!db.pre!]ecms_news_indexbak;

alter table [!db.pre!]ecms_newstemp rename to [!db.pre!]ecms_news_index;

ALTERTABLE`[!db.pre!]ecms_news_index`CHANGE`id“id`INT(
10)NOTNULLAUTO_INCREMENT;

alter table [!db.pre!]ecms_news_index add index(classid);

alter table [!db.pre!]ecms_news_index add index(checked);

alter table [!db.pre!]ecms_news_index add index(newstime);

alter table [!db.pre!]ecms_news_index add index(truetime);

update [!db.pre!]ecms_news_index set checked=
1;