SQLite Guide
SQLAPI++ allows to seamlessly work with a variety of SQL database servers. It provides unified API to access any database, keeping your code portable. 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.
For complete information on using SQLAPI++ check out Getting Started and Documentation. This guide covers specific information related to working with SQLite server using SQLAPI++ library in the following areas:
- Connecting to a database
- Transaction isolation levels
- Working with Long or Lob (CLob, BLob) data
- Returning output parameters
- Cancelling queries
- Connection, command, parameter and field options
- Using native API
- Getting native connection related handles
- Getting native command related handles
- Error handling
Connecting to a database
To connect to a database you need to initialize a connection object. A connection object is represented by SAConnection class.
Minimum Version
After the connection is created you need to call SAConnection::Connect method to establish connection with SQLite server:
void Connect(
const SAString &sDBString,
const SAString &sUserID,
const SAString &sPassword,
SAClient_t eSAClient = SA_Client_NotSpecified);
Parameters
sDBString
A string containing a valid SQLite database file path
sUserID
Not applicable for SQLite, leave empty
sPassword
Not applicable for SQLite, leave empty
eSAClient
SA_SQLite_Client
– SQLite clientSA_Client_NotSpecified
– used by default ifeSAClient
parameter is omitted. You can use this default value only if you have SAConnection::setAPI method with SAPI object initialized withSA_SQLite_Client
constant before
For more details see Getting Started - Connect to Database, SAConnection object, SAConnection::Connect.
Transaction isolation levels
SQL-92 defines four isolation levels, but SQLite does not support the concept of SQL-92 levels, so calling SAConnection::setIsolationLevel has no effect for SQLite.
Instead, SQLite offers three different modes for transactions: "DEFERRED"
, "EXCLUSIVE"
and "IMMEDIATE"
. You can control which one is used with "SQLiteTransactionType"
connection-scoped option.
SQLite Docs
Working with Long or Lob (CLob, BLob) data
When fetching data SQLAPI++ detects data types of the columns in the result set and maps those types to internal library types. The mapping determines which native APIs the library will use for fetching LOB data.
In SQLite, when fetching data, no server data types are mapped into SQLAPI++ Long or Lob types.
When binding input data from your program the reverse mapping is taking place. The SQLAPI++ data type you use for input markers determines what native API program types will be used for sending Long/Lob data to the server.
library types
to SQLite API data types
:SA_dtLongBinary | → | SQLITE_BLOB |
SA_dtLongChar | → | SQLITE_TEXT |
SA_dtBLob | → | SQLITE_BLOB |
SA_dtCLob | → | SQLITE_TEXT |
For additional information see Getting Started - Handle Long/CLob/BLob.
Returning output parameters
SQLAPI++ doesn't support SQLite functions and does not support returning output parameters.
For additional information see SACommand::Execute, SAParam object, Getting Started - Get Output Parameters.
Cancelling queries
Using SACommand::Cancel method you can cancel the following types of processing on a statement:
- function running asynchronously on the statement
- 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 native SQLite documentation.
For additional information see SACommand::Cancel.
Connection, command, parameter and field options
Server specific options can be applied at the API, connection, command, parameter or field levels.
We recommend you specify each option at the appropriate level, although it is possible to specify them at the parent object level as well. In that case the option affects all the child objects.
API level options must be specified in SAPI object. If an internal SAPI object is used for the DBMS API initialization (implicit DBMS API initialization, see SAConnection::Connect method) the related DBMS specific options are taken from the initial connection object.
Connection level options may be specified in either SAPI object or SAConnection object. If specified in SAPI object an option affects all connections on that API.
Command level options may be specified in SAPI object, SAConnection object or SACommand object. If specified in a parent object an option affects all commands on that SAPI or SAConnection object.
Parameter level options may be specified in SAPI object, SAConnection object, SACommand object or SAParam object. If specified in a parent object an option affects all parameters on that SAPI, SAConnection or SACommand object.
Field related options may be specified in SAPI object, SAConnection object, SACommand object or SAField object. If specified in a parent object an option affects all fields on that SAPI , SAConnection or SACommand object.
Specific options applicable to SQLite:
SQLITE.LIBS
- Windows -
"sqlite3.dll"
- Linux -
"libsqlite3.so"
"STATIC"
, forces using the linked SQLite client API functions when the library is compiled with SA_STATIC_SQLITE
build option.SQLiteSkipInitialization
sqlite3_initialize
call when SQLite API loaded. Can be useful when sqlite3_config function
should be called."True"
, "1"
"False"
BusyTimeout
SQLiteVFSName
sqlite3_open_v2()
interface VFS module name. For more information see SQLite documentation.SQLiteVFSFlags
sqlite3_open_v2()
interface VFS flags. For more information see SQLite documentation.SQLiteDateTypes
"DATE,TIME,DATETIME,TIMESTAMP"
SQLiteDateValueType
SA_dtDateTime
parameters."TEXT"
- the datetime values are 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
"TEXT"
SQLiteTransactionType
"DEFERRED"
(default)"EXCLUSIVE"
"IMMEDIATE"
For additional information see SAOptions::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. Note that using appropriate type casting depends on an API (that generally mean that you have to explicitly check client version before casting, see SAConnection::ClientVersion method).
To use native API you need to add SQLite specific #include
and cast the result of SAConnection::NativeAPI to class sl3API
:
#include "sl3API.h"
IsaAPI *pApi = con.NativeAPI();
sl3API *pNativeAPI = (sl3API *)pApi;
To get more information about SQLite API functions see SQLite documentation.
For additional information 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 parameters. 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.
To access native connection handles you need to add SQLite specific #include
and cast the result to class sl3ConnectionHandles
:
#include "sl3API.h"
saConnectionHandles *pHandles = con.NativeHandles();
sl3ConnectionHandles *pNativeHandles = (sl3ConnectionHandles*)pHandles;
To get more information about SQLite API functions and handles see SQLite specific documentation.
For additional information 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 parameters. 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.
To access native command handles you need to add SQLite specific #include
and cast the result to class sl3CommandHandles
:
#include "sl3API.h"
saCommandHandles *pHandles = cmd.NativeHandles();
sl3CommandHandles *pNativeHandles = (sl3CommandHandles*)pHandles;
To get more information about SQLite API functions and handles see SQLite specific documentation.
For additional information see SACommand::NativeHandles.
Error handling
When an error occurs when executing a SQL statement SQLAPI++ library throws an exception of type SAException and SAException::ErrPos method returns error position in the SQL statement.
In SQLite server SAException::ErrPos method returns -1 because SQLite does not support this function.
For additional information see Getting Started - Error Handling, SAException object.