Java sqlite last_insert_rowid()返回0

时间:2015-01-04 10:16:17

标签: java 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 个答案:

答案 0 :(得分:2)

如果在您执行上一次插入的同一个打开连接上调用函数last_insert_rowid(),则返回有效行ID(行ID大于0)。您可以使用相同的连接解决此问题,或者如果您只想从max获取UGYFEL ID,则可以像这样获取

SELECT MAX(YOUR_ID_COL_NAME) AS LAST FROM UGYFEL

答案 1 :(得分: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();