2012-09-13 60 views
0

我无法执行以下代码,显然这些语句是代码的一部分。键'PRIMARY'的重复条目'1'没有解决修复TABLE

产生
cursor.execute("select max(propernoun_SRNO) from tblauto_tagged") 

starting_index = cursor.fetchone()[0] 

if starting_index == None : 

    starting_index = 1 

ending_index = int(starting_index) +len(s) 

i = 0 

for j in range(starting_index,ending_index): 
    for i in range(0,len(s)): 
     c.append(nnp_array_index_coloumn.count(i)) 
     add1 = add1 + c[i-1] 
     add2 = add1 + c[i] 
     cursor.execute("INSERT INTO tblauto_tagged(propernoun_SRNO,tagger,train,propernoun,propernoun_ID) VALUES (?,?,?,?,?)",(j,str(iput),str(corpora),str(nnp_array[add1:add2]),0)) 
     for item in nnp_array_index_coloumn: 
      if item not in uniques: 
       uniques.append(item) 
       add1=0;add2=0 

错误是

IntegrityError: ('23000', "[23000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.61-community]Duplicate entry '1' for key 'PRIMARY' (1062) (SQLExecDirectW)") 

我看了以前的尝试不同的用户来解决这个问题,但对我来说毫无效果的。

mysql> repair table tblauto_tagged; 
+--------------------------------+--------+----------+--------------------------------- ------------------------+ 
| Table       | Op  | Msg_type | Msg_text             | 
+--------------------------------+--------+----------+---------------------------------------------------------+ 
| collegedatabase.tblauto_tagged | repair | note  | The storage engine for the table  doesn't support repair | 
+--------------------------------+--------+----------+---------------------------------------------------------+ 
1 row in set (0.00 sec) 

mysql> select * from tblauto_tagged; 
Empty set (0.00 sec) 

你所有的有益的建议后,我用下面的语句

cursor.execute("INSERT INTO tblauto_tagged(tagger,train,propernoun,propernoun_ID) VALUES (?,?,?,?)",(str(iput),str(corpora),str(nnp_array[add1:add2]),0)) 

我遇到了另一个麻烦,今天肯定不是我day.In为了让我的问题更清楚,我正在编辑一些附加信息的问题。以往的事情工作正常,直到propernoun_SRNO = 149

mysql> select propernoun_SRNO ,tagger, train,propernoun,propernoun_ID from tblauto_tagged where propernoun_SRNO =149; 
+-----------------+--------+-------+------------------------------------------------------------------------------------------------------------+---------------+ 
| propernoun_SRNO | tagger | train | propernoun                         | propernoun_ID | 
+-----------------+--------+-------+------------------------------------------------------------------------------------------------------------+---------------+ 
|    149 | 1  | 1  | ['Wing', 'tank', 'Hopper', 'BU', 'crewmember', 'beam', 'injured', 'Drug', 'Serious', 'Marine', 'Incident'] |    0 | 
+-----------------+--------+-------+------------------------------------------------------------------------------------------------------------+---------------+ 

和propernoun_SRNO = 150之后,这是我所得到的。

mysql> select propernoun_SRNO ,tagger, train,propernoun,propernoun_ID from tblauto_tagged where propernoun_SRNO =150; 
+-----------------+--------+-------+------------+---------------+ 
| propernoun_SRNO | tagger | train | propernoun | propernoun_ID | 
+-----------------+--------+-------+------------+---------------+ 
|    150 | 1  | 1  | []   |    0 | 
+-----------------+--------+-------+------------+---------------+ 
1 row in set (0.00 sec) 

肚里一路下跌到propernoun_SRNO = 22201

mysql> select max(propernoun_SRNO) from tblauto_tagged; 
+----------------------+ 
| max(propernoun_SRNO) | 
+----------------------+ 
|    22201 | 
+----------------------+ 
1 row in set (0.00 sec) 

mysql> select propernoun_SRNO ,tagger, train,propernoun,propernoun_ID from tblauto_tagged where propernoun_SRNO =22201; 
+-----------------+--------+-------+------------+---------------+ 
| propernoun_SRNO | tagger | train | propernoun | propernoun_ID | 
+-----------------+--------+-------+------------+---------------+ 
|   22201 | 1  | 1  | []   |    0 | 
+-----------------+--------+-------+------------+---------------+ 
1 row in set (0.00 sec) 

我不知道是什么问题,但我认为它需要一些专家的意见。正如先前的评论我应该用序列所提到的,请指教

+6

**从不**手动计算下一个PK值。改为使用自动增量列或序列。 – zerkms

+0

@zerkems,+1,但有系统(坏系统,但你必须忍受的系统),例如,多个表都使用相同的ID作为他们的PK,而不是用FKs做正确的方式,但aren不要总是插在一起,并按顺序。尽管如此,这看起来并不是那种情况之一。 –

回答

0

为了解释zerkms的评论:您试图插入行与主键的值(propernoun_SRNO,如果我没有记错的话),这是已在使用中。

如果您尝试将所有新行插入到数据库中,请不要指定主键的值,并且MySQL应自动计算一个值,假设该列设置为AUTO_INCREMENT

如果您试图用这些ID替换现有的行,请使用REPLACE而不是INSERT,或者在插入新行之前删除现有行。

+0

以及我尝试使用建议的zerkms的建议它的工作,但在propernoun_SRNO = 148后;没有记录。在记录数字148之后,根本没有数值。我认为这是因为我的BAD编程技术;但无论如何,评论对帮助我理解这个问题非常有用。我会尝试按照zerkms的建议使用序列。 –

相关问题