2012-09-13 58 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的建議使用序列。 –

相關問題