SQLLastID

From SCAR Divi Manual
Revision as of 23:49, 9 February 2012 by Freddy (talk | contribs) (Created page with "==Definition== <source lang="scar" lines="false"> function SQLLastID(const DBIndex: Integer): Integer; </source> ==Availability== SCAR Divi 3.31 > Current ==Description== Retur...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Definition

function SQLLastID(const DBIndex: Integer): Integer;

Availability

SCAR Divi 3.31 > Current

Description

Returns the ID of the last row that was inserted into an open SQLite3 database given by DBIndex.

Example

var
  DB, Query, InsertID: Integer;

begin
  DB := SQLOpen(WorkspacePath + 'test.db3', True);
  try
    // Create a table
    SQLQuery(DB, 'CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, string TEXT)');

    // Insert 2 rows into the table
    Query := SQLQueryEx(DB, 'INSERT INTO test VALUES(NULL, ?)');
    try
      SQLBindStr(Query, 1, 'Hello Mars!');
      SQLQueryStep(Query);
      SQLQueryReset(Query);
      SQLBindStr(Query, 1, 'Hello World!');
      SQLQueryStep(Query);
      InsertID := SQLLastID(DB);
    finally
      SQLQueryFree(Query);
    end;

    // Get the data stored in the last row
    Query := SQLQueryEx(DB, 'SELECT * FROM test WHERE id = ?');
    try
      SQLBindInt(Query, 1, InsertID);
      if SQLQueryStep(Query) = SQL_ROW then
        WriteLn(SQLColumnStr(Query, 1));
    finally
      SQLQueryFree(Query);
    end;
  finally
    SQLFree(DB);
  end;
end.

Output:

Hello World!

See Also