Connect to SQLite database (FireDAC)
Go Up to Database Connectivity (FireDAC)
This topic describes how to connect to SQLite database files.
The FireDAC native driver supports SQLite database version 3.0 and later. For a detailed discussion on SQLite usage in FireDAC for a Object Pascal application, read the "Using SQLite with FireDAC" article.
Windows Client Software
FireDAC supports two SQLite library linking modes:
- Static linking -- the x86 sqlite3_x86.obj or x64 sqlite3_x64.obj client library is statically linked into the application.
FireDAC provides sqlite3_Xxx.obj v 3.8.3. That is the default mode, no additional files or actions are required. Note that the SQLite database encryption is supported only for static linking.
- Dynamic linking -- the x86 or x64 SQLITE3.DLL client library must be available in order to open a SQLite database. The recommended SQLITE3.DLL versions are 220.127.116.11 or later. That is the default mode for non-Windows platforms. Note that for Appmethod C++ x64 mode the dynamic linking is used.
You can download:
- The latest x86 DLL version from here, Chapter "Precompiled Binaries For Windows", item "This is a DLL" and place it in a folder listed in your PATH environment variable (for example, System32 folder) or in your application EXE folder.
- The x64 DLL version from here as "sqlite-netFx40-binary-x64-xxxxx.zip". Extract to a folder, copy SQLite.Interop.DLL into SQLITE3.DLL, then place it as above.
To choose the linking mode, in $(BDS)\source\data\firedac\FireDAC.inc:
- define FireDAC_SQLITE_STATIC for static linking.
- undefine FireDAC_SQLITE_STATIC for dynamic linking.
If the SQLite client library has not been installed properly, an exception is raised when you try to connect:
[FireDAC][Phys][SQLite]-314. Cannot load vendor library [SQLITE3.DLL]. The specified module could not be found. Check [SQLITE3.DLL], which is located in one of the PATH directories or in the application EXE directory.
Linux Client Software
FireDAC on Linux supports only dynamic linking and requires:
- the libsqlite3.so x86 or x64 engine.
To install on Linux use the commands:
sudo apt-get update sudo apt-get install sqlite3 libsqlite3-dev
Mac OS X and iOS Client Software
FireDAC on Mac OS X and iOS supports only dynamic linking and requires:
- the libsqlite3.dylib x86 engine.
It comes pre-installed on Mac OS X and iOS. Note that the default Mac OS X and iOS libsqlite3.dylib are compiled with limited column metadata capabilities (SQLITE_ENABLE_COLUMN_METADATA is undefined). As a result, FireDAC may fail to detect column auto-incremental mode and optionality.
To link the driver:
- drop a TFDPhysSQLiteDriverLink component from the "FireDAC Links" palette page
- or include the FireDAC.Phys.SQLite unit in an uses clause.
Connection Definition Parameters
To connect to a SQLite database, most applications require you to specify DriverID and Database.
A path to a database. Use ':memory:' or an empty string to create and connect to an empty in-memory database. A path can include path variables.
A mode to open a database:
|Encrypt||Specifies a default encryption mode for a database. The mode may be overridden with an optional password prefix. If it is not specified, then the mode specified by this parameter is used. Otherwise, aes-256 is used. For more details, read this.|
|Password||Specifies a password for an encrypted database. The value may have the following form:
[ aes-128 | aes-192 | aes-256 | aes-ctr-128 | aes-ctr-192 | aes-ctr-256 | aes-ecb-128 | aes-ecb-192 | aes-ecb-256 :] <password> The optional prefix controls the cipher algorithm to be used. The default value is an empty string, which means the unencrypted mode.
Specifies a new password for a database and performs an encryption operation:
|BusyTimeout||Sets an "ms" milliseconds to sleep when a table is locked and UpdateOptions.LockWait is set to True. Zero means do not wait. The default value is 10000.||5000|
|CacheSize||Changes the maximum number of database disk pages that SQLite holds in memory at once. Each page uses about 1.5K of memory. The default value is 10000.||10000|
|SharedCache||Enables or disables the SQLite shared cache feature. For more details, read this. The default value is True.||False|
Sets the database connection locking-mode. The value is one of the following:
The default value is Exclusive, because it allows you to get maximum read/write speed for single user applications.
Sets the database connection synchronization mode of the in-memory cache with database files. The value is one of the following:
Enables foreign key usage for the database connection, when the application uses SQLite v 3.6.19 or later. The value is one of the following:
Defines how to represent String values:
Defines how to store GUID values:
Defines how to store date and time values:
Enables, disables, or specifies the SQLite engine extensions to load:
|SQLiteAdvanced||Additional SQLite database connection options. For details, see the Pragma statements supported by SQLite.||auto_vacuum = 1;page_size = 4096;temp_store = FILE|
|MetaDefCatalog||Default database name. The Design time code excludes the catalog name from the object name if it is equal to MetaDefCatalog. Setting MetaDefCatalog does not change the current database in the SQLite session. The default value is 'MAIN'.||MyDB|
- Connect to a local database in exclusive mode:
- Connect to a shared database (it is not recommended to store SQLite databases on WinNT shared folders for multi user read-write access):
DriverID=SQLite Database=\\srv\mydb.sqlite LockingMode=Normal Synchronous=Normal
- Encrypt an unencrypted database:
DriverID=SQLite Database=$(FDHOME)\DB\Data\ADDemo.sdb NewPassword=aes-256:123qwe
- Open an encrypted database:
DriverID=SQLite Database=c:\temp\test.db Password=123qwe
- Connect to an in-memory database:
- Connect to a database file in the Documents folder on the iOS device or simulator in exclusive mode: