Difference between revisions of "SQLOpen"

From SCAR Divi Manual
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 8: Line 8:
  
 
==Description==
 
==Description==
Opens an [http://www.sqlite.org/ SQLite3] database at the path given by '''Path'''. If '''CreateIfNotFound''' is true, it will create a new database at the given path if none is found. The function returns the index for the database in the resource system or -1 if the function failed or in case '''CreateIfNotFound''' is [[false]], when there was no file at the given path.
+
Opens an [[SQLite3]] database at the path given by '''Path'''. If '''CreateIfNotFound''' is [[true]], it will create a new database at the given path if none is found. The function returns the index for the database in the resource system or -1 if the function failed or in case '''CreateIfNotFound''' is [[false]], when there was no file at the given path. You can free an open database connection with [[SQLFree]].
  
 
==Example==
 
==Example==
Line 16: Line 16:
  
 
begin
 
begin
   DB := SQLOpen(LogsPath + 'test.db3', True);
+
   DB := SQLOpen(WorkspacePath + 'test.db3', True);
 
   try
 
   try
 
     // Create a table
 
     // Create a table

Latest revision as of 10:01, 10 February 2012

Definition

function SQLOpen(const Path: string; const CreateIfNotFound: Boolean): Integer;

Availability

SCAR Divi 3.31 > Current

Description

Opens an SQLite3 database at the path given by Path. If CreateIfNotFound is true, it will create a new database at the given path if none is found. The function returns the index for the database in the resource system or -1 if the function failed or in case CreateIfNotFound is false, when there was no file at the given path. You can free an open database connection with SQLFree.

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