Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Getting the ID of an INSERT in mysql

  • 24-01-2007 12:01PM
    #1
    Registered Users, Registered Users 2 Posts: 378 ✭✭


    I'm using a JDBC connector to talk to "MySQL 4.1.14"

    At the moment to return the ID I am performing an Insert followed by

    SELECT MAX(ID) FROM TABLE;

    This works fine for now but it is not future proof... obviously if a transaction happens before I call the MAX(ID) then this will not return the ID for the original Insert.

    SELECT LAST_INSERT_ID(); only returns 0 for me... but it would have the same result as above, with increased traffic errors will occur.

    So basically, does anyone know how to return the value of the autoincrement column of the record you are inserting?


Comments

  • Registered Users, Registered Users 2 Posts: 6,680 ✭✭✭daymobrew




  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    If the table has a column with AUTO_INCREMENT then and stmt is a prepared statement which is an insert for that table then:
        public int performInsert(PreparedStatement stmt) {
            
            try{
                stmt.executeUpdate();
    
                 ResultSet rs = stmt.getGeneratedKeys();
                 if (rs.next()) {
                     return rs.getInt(1);
                 } else
                     throw new RuntimeException("MYSQL : No generated keys.");
            } catch (SQLException ex){
                throw new RuntimeException("SQLINSERT[" + stmt.toString() + "]",ex);
            }
        }
    


  • Registered Users, Registered Users 2 Posts: 32,132 ✭✭✭✭is_that_so


    You might find the code below useful as well.
    function insertRec($db_query)
    {
    mysql_query("LOCK TABLES table_name WRITE;");
    mysql_query($db_query); //table insert here
    $ID=mysql_insert_id();
    mysql_query("UNLOCK TABLES");
    return $ID;
    }
    


  • Registered Users, Registered Users 2 Posts: 3,890 ✭✭✭cgarvey


    sicruise wrote:
    So basically, does anyone know how to return the value of the autoincrement column of the record you are inserting?
    INSERT INTO user( id, name ) VALUES( NULL, 'user1' );
    SELECT MAX( last_insert_id() ) FROM user;
    

    last_insert_id() is connection/session-safe.


  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


    Thanks a million for that PH ... I never noticed that method before...

    Works a treat!

    Thanks everyone else too... was racking my brain for a good while on this


  • Advertisement
Advertisement