SQLite Guide

SQLAPI++ allows to work with a number of SQL database servers. It provides common mechanisms to access database, and as a general rule they work for any database server. But each server has some specific features which a developer has to know in order to leverage server's unique features and avoid potential errors.

This page collects all specific information that concerns working with SQLite database using SQLAPI++ Library. Full information about using SQLAPI++ see in How To and Documentation.

Available topics:

Connecting to a database

To connect to a database you should create a connection object and then connect it. A connection object is represented by SAConnection class. After the connection is created you need to connect it to SQLite server using SAConnection::Connect method:

void Connect(
    const SAString &sDBString,
    const SAString &sUserID,
    const SAString &sPassword,
    SAClient_t eSAClient = SA_Client_NotSpecified);
Parameters
sDBStringA string containing a valid SQLite database file path
sUserIDThis parameter isn't used SQLAPI++ with SQLite
sPasswordThis parameter isn't used SQLAPI++ with SQLite
eSAClientOptional. One of the following values from SAClient_t enum:
  • SA_SQLite_Client SQLite client
  • SA_Client_NotSpecified Used by default if eSAClient parameter is omitted. You can use this default value only if you have SAConnection::setAPI method with SAPI object initialized with SA_SQLite_Client constant before

The SQLAPI++ Library requires libsqlite version 3.x or higher.

For more details see How To - Connect to database, SAConnection object, SAConnection::Connect.

Transaction isolation levels

SQL-92 defines four isolation levels, all of which are supported by SQLAPI++. But SQLAPI++ does nothing with these isolation levels for SQLite. It only supports 'BEGIN EXCLUSIVE' and 'BEGIN IMMEDIATE' transaction blocks.

Working with Long or Lob (CLob, BLob) data

SQLAPI++ supports four types for working with Long or Lob (CLob, BLob) data:

NameC++ Enum
LongBinarySA_dtLongBinary
LongCharSA_dtLongChar
BLob (Binary Large object)SA_dtBLob
CLob (Character Large object)SA_dtCLob

The table below shows how SQLAPI++ data types correspond with servers original data types:

SA_dtLongBinary => SQLITE_BLOB
SA_dtLongChar => SQLITE_TEXT
SA_dtBLob => SQLITE_BLOB
SA_dtCLob => SQLITE_TEXT

For more details see How To - Working with Long or Lob (CLob, BLob) data.

Returning output parameters

SQLAPI++ doesn't support SQLite functions and does not support returning output parameters.

Cancelling queries

Using SACommand::Cancel method you can cancel the following types of processing on a statement:

  • A function running asynchronously on the statement.
  • A function running on the statement on another thread.

SQLAPI++ calls sqlite3_interrupt function to cancel a query. To get more details see sqlite3_interrupt function description in SQLite documentation.

For more details see SACommand::Cancel.

Connection, command, parameter and field options

A server specific option can relate to a connection, command, parameter or field. We recommend you specify each option in an appropriate object, although it is possible to specify them in the parental object as well. In that case the option affects all the child objects.

An API related option must be specified in SAPI object. If an internal SAPI object used for the DBMS API initialization (implicit DBMS API initialization, see SAConnection::Connect method) the related DBMS specific options taken from initial connection object.

A connection related option may be specified in either SAPI object or SAConnection object. If it is specified in SAPI object it affects all the connections on that API.

A command related option may be specified in SAPI object, SAConnection object or SACommand object. If it is specified in a parent object it affects all the commands on that SAPI, SAConnection objects.

A parameter related option may be specified in SAPI object, SAConnection object, SACommand object or SAParam object. If it is specified in a parent object it affects all the parameters on that SAPI, SAConnection, SACommand objects.

A field related option may be specified in SAPI object, SAConnection object, SACommand object or SAField object. If it is specified in a parent object it affects all the fields on that on that SAPI, SAConnection, SACommand objects.

Specific options for SQLite:

Option name / ScopeDescription

SQLITE.LIBS

API related. Should be specified before the API is initialized.

Forces SQLAPI++ Library to use specified SQLite library.

Valid values: Any valid SQLite library name list. Names separated by ';' on Windows or ':' on other operating systems

Default value:

  • Windows - "sqlite3.dll"
  • Linux - "libsqlite3.so"

Special value: "STATIC", forces using the linked SQLite API functions when the Library compiled with SA_STATIC_SQLITE build option

SQLiteSkipInitialization

API related. Should be specified before the API is initialized.

Forces SQLAPI++ to skip sqlite3_initialize call when SQLite API loaded. Can be useful when sqlite3_config function should be called.

Valid values: "True", "1"

Default value: "False"

BusyTimeout

Connection related. Should be specified before connection is made.

Sets a busy handler that sleeps for a specified amount of time when a table is locked. For more information see SQLite documentation.

Valid values: String represents busy timeout (milliseconds)

Default value: none

SQLiteVFSName

Connection related. Should be specified before connection is made.

Sets sqlite3_open_v2() interface VFS module name. For more information see SQLite documentation.

Valid values: Any string represents VFS module name

Default value: none

SQLiteVFSFlags

Connection related. Should be specified before connection is made.

Sets sqlite3_open_v2() interface VFS flags. For more information see SQLite documentation.

Valid values: Any numeric string represents VFS flags

Default value: none

SQLiteDateTypes

Connection related

Sets a comma separated list of database field types those should be interpreted as a datetime fields.

Valid values: Any suitable data type list

Default value: "DATE,TIME,DATETIME,TIMESTAMP"

SQLiteDateValueType

Connection or command related

Sets a program type of the SA_dtDateTime parameters.

Valid values: "TEXT" - the datetime values formatted into "YYYY-MM-DD HH:MM:SS.SSS" strings, "DOUBLE" - the datetime vlaue is the Julian Day (JD) number expressed as a floating point value

Default value: "TEXT"

SQLiteTransactionType

Connection related

Sets SQLite transaction type.

Valid values: "EXCLUSIVE", "IMMEDIATE", "DEFFERED" (default)

Default value: none

For more details see SAConnection::setOption, SACommand::setOption, SAField::setOption, SAParam::setOption.

Using native SQLite API

You can call client specific API functions which are not directly supported by SQLAPI++ Library. SAConnection::NativeAPI method returns a pointer to the set of native API functions available for SQLite. To use the database API directly you have to downcast this IsaAPI pointer to the appropriate type and use its implementation-specific members. The following example shows what type cast you have to make and what additional header file you have to include to work with SQLite API.

Cast the result to class sl3API:

#include "sl3API.h"
 
IsaAPI *pResult = con.NativeAPI();
sl3API *p_sl3API = (sl3API *)pResult;

To get more information about DBMS API functions see this DBMS specific documentation.

For more details see SAConnection::NativeAPI.

Getting native SQLite connection related handles

You have to use native API handles when you want to call specific SQLite API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). SAConnection::NativeHandles method returns a pointer to the set of native API connection related handles. To use API handles directly you have to downcast saConnectionHandles pointer to the appropriate type and use its implementation-specific members. The following example shows what type cast you have to make and what additional header file you have to include to work with specific SQLite API.

Cast the result to class sl3ConnectionHandles:

#include "sl3API.h"
 
saConnectionHandles *pResult = con.NativeHandles();
sl3ConnectionHandles *p_sl3CH = (sl3ConnectionHandles*)pResult;

To get more information about DBMS API functions and handles see this DBMS specific documentation.

For more details see SAConnection::NativeHandles.

Getting native SQLite command related handles

You have to use native API handles when you want to call specific SQLite API functions which are not directly supported by the Library. API functions usually need to receive one or more active handles as a parameter(s). SACommand::NativeHandles method returns a pointer to the set of native API command related handles. To use API handles directly you have to downcast saCommandHandles pointer to the appropriate type and use its implementation-specific members. The following eaample shows what type cast you have to make and what additional header file you have to include to work with specific SQLite API.

Cast the result to class sl3CommandHandles:

#include "sl3API.h"
 
saCommandHandles *pResult = cmd.NativeHandles();
sl3CommandHandles *p_sl3CH = (sl3CommandHandles*)pResult;

To get more information about DBMS API functions and handles see this DBMS specific documentation.

For more details see SACommand::NativeHandles.

Error handling

When an error occurs inside SQLAPI++ Library it throws an exception of type SAException.

SAException::ErrPos method gets an error position in SQL statement. In SQLite server SAException::ErrPos method returns -1 because SQLite does not support this function.

For more details see How To - Error handling, SAException object.

Special header files - Compile time

The header files are in the include subdirectory of SQLAPI++ distributions:

  • #include "SQLAPI.h" - main header, should be used whenever SQLAPI++ is used
  • #include "sl3API.h" - SQLite API, should be included if direct SQLite calls are required

For more details see Documentation - Compiling and Linking Applications with SQLAPI++