AnyDAC
ContentsIndexHome
PreviousUpNext
Connect to SQLite database

Describes how to connect to SQLite database files.

Group
Links
Supported versions

The AnyDAC native driver supports SQLite database version 3.0 and higher. 

Read "Using SQLite with AnyDAC" chapter for detailed discussion of SQLite usage in AnyDAC for Delphi application. 

 

Windows client software

AnyDAC supports two SQLite library linking modes:

  • Static linking: the x86 sqlite3_x86.obj or x64 sqlite3_x64.obj client library is statically linked into application. AnyDAC provides sqlite3_Xxx.obj v 3.7.15. That is default mode, no additional files or actions is required.
  • Dynamic linking: the x86 or x64 SQLITE3.DLL client library must be available to open a SQLite database. The recommended SQLITE3.DLL versions is 3.7.7.1 or higher. That is default mode for x64.

You can download:

  • the latest x86 DLL version from there, Chapter "Precompiled Binaries For Windows", item "This is a DLL" and put it into a folder listed in your PATH environment variable (for example, <System32> folder) or into your application EXE folder. AnyDAC Installer installs SQLITE3.DLL v 3.7.15 into your windows\system32 folder.
  • the x64 DLL version from there as "sqlite-netFx40-binary-x64-xxxxx.zip". Extract to a folder, copy SQLite.Interop.DLL into SQLITE3.DLL, then put it as above. AnyDAC Installer installs x64 SQLITE3.DLL v 3.7.15 into your windows\system32 folder.

To choose linking mode, in AnyDAC\Source\uAD.inc:

  • define AnyDAC_SQLITE_STATIC for static linking;
  • undefine AnyDAC_SQLITE_STATIC for dynamic linking.

When the SQLite client library has not been properly installed, you will get an exception when you try to connect: 

 

[AnyDAC][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

AnyDAC 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 client software

AnyDAC on Mac OS X supports only dynamic linking and requires:

  • the libsqlite3.dylib x86 engine.

It comes preinstalled on Mac OS X. Note, the default Mac OS X libsqlite3.dylib is compiled with limited column metadata capabilities. As result, AnyDAC may fail to detect column auto-incremental mode and optionality. 

 

Driver linkage

To link the driver:

 

Connection definition parameters

To connect to a SQLite database, most applications will need to specify DriverID and Database

 

DriverID=SQLite  

Parameter 
Description 
Example value 
Database 
A path to a database. The path value may include environment variables: $(<variable>).
Use ':memory:' or empty string to create and connect to empty in-memory database. 
  • c:\MyApp\db.sdb
  • $(temp)\db.sdb
 
OpenMode 
A mode to open a database:
  • CreateUTF8 - open a database to read or write. If the database does not exist, it will be created with the UTF8 default encoding (the default value for pre-Delphi 2009).
  • CreateUTF16 - open a database to read or write. If the database does not exist, it will be created with the UTF16 default encoding (the default value for Delphi 2009 and higher).
  • ReadWrite - open a database to read or write. If the database does not exist, an exception will be raised.
  • ReadOnly - open a database to read only. If the database does not exist, an exception will be raised.
 
ReadOnly 
Encrypt 
Specified a default encryption mode for a database. The mode may be overridden by an optional password prefix. If it is not specified, then will be used mode, specified by this parameter. Otherwise will be used aes-256. Please, read this for more details. 
 
Password 
Specifies a password for an encrypted database. The value may have a 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>
There optional prefix controls the cipher algorithm to be used.
The default value is empty string, which means unencrypted mode. 
  • aes-256:12345
  • qwe12345qwe
 
NewPassword 
Specifies a new password for a database and performs an encryption operation:
  • to encrypt unencrypted database, specify non-empty NewPassword and empty Password;
  • to decrypt encrypted database, specify empty NewPassword and non-empty Password;
  • to change encrypted database password, specify non-empty NewPassword and non-empty Password.
 
 
BusyTimeout 
Sets a "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 will hold 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. Please, read this for more details. The default value is True. 
False 
LockingMode 
Sets the database connection locking-mode. The value is one of:
  • Normal. This mode gives multi-user access to database files.
  • Exclusive. This mode gives maximum performance.
The default value is Exclusive, because it allows to get maximum read/write speed for single user applications. 
Exclusive 
Synchronous 
Sets the database connection synchronization mode of in-memory cache with database files. The value is one of:
  • Full. Synchronizes at every critical moment.
  • Normal. As above, but less often.
  • Off. Gives maximum performance. This is the default value.
 
Off 
ForeignKeys 
Enables foreign key usage for the database connection, when the application is using SQLite v 3.6.19 or higher. The value is one of:
  • On. Foreign keys in a session are enabled. This is the default value.
  • Off. Foreign keys in a session are disabled.
 
Off 
StringFormat 
Defines how to represent String values:
  • Choose - represent as ftString / ftWideString / ftMemo / ftWideMemo, depending on the declared data type name (default);
  • Unicode - always represent as ftWideString / ftWideMemo;
  • ANSI - always represent as ftString / ftMemo.
 
Unicode 
GUIDFormat 
Defines how to store GUID values:
  • String - stores GUID as a character string value (default).
  • Binary - stores GUID as a binary string value.
 
Binary 
DateTimeFormat 
Defines how to store date and time values:
  • String - stores date and time as a character string value, using the YYYY-MM-DD and HH:MM:SS.XXX format (default).
  • Binary - stores date and time as a real number, which is a Julian date.
  • DateTime - stores date and time as a real number, which is a TDateTime value.
 
Binary 
Extentions 
Enables, disables or specifies the SQLite engine extensions to load:
  • True - enables extensions.
  • False - disables extensions (default).
  • Otherwise use a list of extensions to load in the form <library>[=<entry point][;...].
 
MyExt.dll;FullTS.dll 
SQLiteAdvanced 
Additional SQLite database connection options. See the Pragma statements supported by SQLite for details. 
auto_vacuum = 1;page_size = 4096;temp_store = FILE 
MetaDefCatalog 
Default database name. Design time code will exclude 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 

 

Usage cases
  • Connect to a local database in exclusive mode:

 

DriverID=SQLite
Database=$(ADHOME)\DB\Data\ADDemo.sdb

 

  • 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 unencrypted database:

 

DriverID=SQLite
Database=$(ADHOME)\DB\Data\ADDemo.sdb
NewPassword=aes-256:123qwe

 

  • Open encrypted database:

 

DriverID=SQLite
Database=c:\temp\test.db
Password=123qwe

 

  • Connect to in-memory database:

 

DriverID=SQLite
Database=:memory:
What do you think about this topic? Send feedback!