2012-03-30 63 views
9

Scenerio:MySQL如何在使用INSERT IGNORE INTO時檢索LAST_INSERT_ID的ID?

  1. 我插入CSV文件到我的數據庫。
  2. 我使用JDBC驅動程序來遍歷文件和做我的刀片
  3. 一個name列成爲unique所以當相同的值到達
  4. 我使用INSERT INTO IGNORE保持刀片不改表從當事件發生
  5. 我使用LAST_INSERT_ID()在插入的下一個表添加爲FK突破迭代
  6. 我得到0時INSERT_IGNORE發生

我該如何解決這個問題? CSV文件的

Connection con = null; 

    try 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager.getConnection(URL, USERNAME, PASSWORD); 
     con.setAutoCommit(false); 

     Statement s1 = (Statement) con.createStatement(); 
     s1.executeUpdate("INSERT IGNORE INTO ORIGIN (ORI_NAME) VALUES(\"" 
      + d.getOriginName() + "\")"); 

     Statement s2 = (Statement) con.createStatement(); 
     s2.executeUpdate("INSERT IGNORE INTO STOCK (ORI_ID,S_TITLE) VALUES (" 
      + "(SELECT ORI_ID FROM ORIGIN WHERE ORI_ID =LAST_INSERT_ID()),\"" 
      + d.getStockTitle() + "\")"); 

    } 

     con.commit(); 

    } 
    catch (Exception e) 
    { 
     if (con != null) 
     try 
     { 
      con.rollback(); 
     } 
     catch (SQLException e1) 
     { 
      e1.printStackTrace(); 
     } 

     e.printStackTrace(); 
    } 
    finally 
    { 
     if (con != null) 
     try 
     { 
      con.close(); 
     } 
     catch (SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

例被插入到數據庫

US, P1, M, 200, 344 
US, P1, L, 233, 344 
US, P2, M, 444, 556 
MX, Q4, L, 656, 777 

表:http://sqlfiddle.com/#!2/b5752

+0

Wha t是你想解決的問題?如果您正在執行「INSERT IGNORE」並忽略,則沒有插入,因此沒有該事務的Last_insert_id。如果您需要上一個插入ID,則需要在插入後立即捕獲它。但對於最近的插入嘗試,如果沒有發生,last_insert_id將爲0. – 2012-03-30 12:57:13

+0

@DMac那麼當不想在現有列已經存在的情況下替換現有列時,如何插入數據庫? – stackoverflow 2012-03-30 13:12:59

+0

@stackoverflow我已經發布了一個適合mysql的答案。 – 2012-05-12 21:43:31

回答

2

如果你想獲得一個ID爲後續的插入,你的代碼必須測試LAST_INSERT_ID的0值,如果它得到它,請在表上做一個SELECT來爲插入的FK找到正確的值。

在僞代碼,這將是這樣的:

$err = mysql("insert ignore into mytable values ($x, $y)") 
if($err){ 
    do something for an error 
} 
$id = mysql("select last_insert_id()"); 
if($id == 0){ 
    $id = mysql("select id from othertable where condition is true for the record I want to reference in my insert"); 
    if($id == 0){ 
     do something for error 
    } 
} 
mysql("insert into othertable VALUES ($id, other stuff)") 

你必須把這種對自己的應用程序和語言。

+0

繼承人我的表結構http://sqlfiddle.com/#!2/b5752,我會在上面的編輯提供一個csv文件的例子 – stackoverflow 2012-03-30 13:13:31

5

您是否熟悉MySQL的ON DUPLICATE KEY UPDATE語法?

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

// Results are the same as doing an update. 
// Note the way the PK can be used to avoid an IGNORE. 
s1.executeUpdate("INSERT INTO ORIGIN (ORI_NAME) VALUES(\"" 
      + d.getOriginName() + "\" ON DUPLICATE KEY UPDATE ORI_ID=ORI_ID)"); 

一之實踐例子這裏涉及失敗的電子郵件地址的網站維護:

http://blog.developpez.com/james-poulson/p10016/sql/ne-rien-modifier-en-cas-de-duplicate-key/

至於檢索最後插入的ID是從MySQL採取以下.com以上鍊接:

Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows: 

INSERT INTO table (a,b,c) VALUES (1,2,3) 
    ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3; 
+2

這應該是肯定的接受答案。完美的工作,並節省了很多其他必需的邏輯,例如在接受的答案中提供的邏輯。 – teh1 2013-03-29 22:42:50