Returns the object ID of the most recent inserted row if supported by the database.


last_insert = dbQueryGetLastInsertID(qid)
Parameters:qid (scalar) – query number.
Returns:last_insert (scalar) – object id


** Given NAMES is an empty MySQL
** table with the *id* column
** auto-incrementing.
// Add `MYSQL` to list of database connections
db_id = dbAddDatabase("MYSQL");

// Create and prepare query
qid = dbCreateQuery(db_id, "INSERT
    INTO NAMES (first, last) VALUES
    ('John', 'Doe');");

** Check if database supports ID of last
** inserted row
if dbHasFeature(db_id, "LastInsertId");

    // Get ID of last inserted row
    last_id = dbQueryGetLastInsertID(qid);



If more than one row was touched by the insert, the behavior is undefined.

For MySQL databases the row’s auto-increment field will be returned.

With a PSQL database, the table must contain OID’s which were not created by default. Check the default_with_oids configuration variable to be sure.

See also

Functions dbHasFeature()