SQLQueryEx

From SCAR Divi Manual
Jump to: navigation, search

Definition

function SQLQueryEx(const DBIndex: Integer; const Query: string): Integer;

Availability

SCAR Divi 3.31 > Current

Description

Creates a new SQLite3 query statement Query for the database given by DBIndex. The function returns an index to the query statement in the system. To execute the query, you have to call SQLQueryStep, it may be required to bind parameters to the query first. You can add parameters with the "?" sign, then fill them out later using SQLBindX methods, the parameters are offset at index 1 rather than 0. An example would be "SELECT * FROM table WHERE id = ?", you would then use SQLBindInt to fill out the integer id parameter at index 1. After you've executed the query, you can bind new parameters and execute it again by calling SQLQueryReset first.

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