2015-01-04 91 views
0

我有一個last_insert_rowid()的問題。它總是返回0Java的sqlite last_insert_rowid()返回0

String query = "SELECT last_insert_rowid() AS LAST FROM UGYFEL"; 
PreparedStatement pst1 = connection.prepareStatement(query); 
ResultSet rs1 = pst1.executeQuery(); 
JOptionPane.showMessageDialog(null, rs1.getString("LAST")); 
pst1.execute(); 

//JOptionPane.showMessageDialog(null, "Adat elmentve"); 

pst1.close(); 
rs1.close(); 

回答

2

功能last_insert_rowid()返回有效的行ID(行ID大於0)如果與最後一次執行插入相同的開放連接上調用。您可以通過使用相同的連接解決它,或者如果你只是希望從UGYFEL得到max ID,就可以得到像這樣

SELECT MAX(YOUR_ID_COL_NAME) AS LAST FROM UGYFEL 
1

所以正常工作。正確?

   String query = "SELECT MAX(ID) AS LAST FROM UGYFEL"; 
       PreparedStatement pst1 = connection.prepareStatement(query); 
       ResultSet rs1 = pst1.executeQuery(); 
       String maxId= rs1.getString("LAST"); 
       //Max Table Id Convert to Integer and +1 
       int intMaxId =(Integer.parseInt(maxId))+1; 
       //Convert to String 
       String stringMaxId = Integer.toString(intMaxId); 
       tUazon.setText(stringMaxId); 
       pst1.execute(); 

       //JOptionPane.showMessageDialog(null, "Adat elmentve"); 

       pst1.close(); 
       rs1.close();