# SQLite RDBMS

OGR optionally supports spatial and non-spatial tables stored in SQLite 3.x database files. SQLite is a "light weight" single file based RDBMS engine with fairly complete SQL semantics and respectible performance.

The driver looks for a geometry_columns table layed out as defined loosely according to OGC Simple Features standards, particularly as defined in FDO RFC 16. If found it is used to map tables to layers.

If geometry_columns is not found, each table is treated as a layer. Layers with a WKT_GEOMETRY field will be treated as spatial tables, and the WKT_GEOMETRY column will be read as Well Known Text geometry.

If geometry_columns is found, it will be used to lookup spatial reference systems in the spatial_ref_sys table.

The SQLite database is essentially typeless, but the SQLite driver will attempt to classify attributes field as text, integer or floating point based on the contents of the first record in a table. None of the list attribute field types existing in SQLite.

SQLite databases often due not work well over NFS, or some other networked file system protocols due to the poor support for locking. It is safest to operate only on SQLite files on a physical disk of the local system.

SQLite is an optionally compiled in driver. It is not compiled in by default.

While the SQLite driver supports reading spatial data from records, there is no support for spatial indexing, so spatial queries will tend to be slow. Attributes queries may be fast, especially if indexes are built for appropriate attribute columns using the "CREATE INDEX ON ( )" SQL command.

By default, SQL statements are passed directly to the SQLite database engine. It's also possible to request the driver to handle SQL commands with OGR SQL engine, by passing "OGRSQL" string to the ExecuteSQL() method, as name of the SQL dialect.

Starting with OGR 1.8.0, the OGR_SQLITE_SYNCHRONOUS configuration option has been added. When set to OFF, this issues a 'PRAGMA synchronous = OFF' command to the SQLite database. This has the advantage of speeding-up some write operations (e.g. on EXT4 filesystems), but at the expense of data safety w.r.t system/OS crashes. So use it carefully in production environments and read the SQLite related documentation.

## VSI Virtual File System API support

(Require OGR >= 1.9.0 and SQLite >= 3.6.0)

The driver supports reading and writing to files managed by VSI Virtual File System API, which include "regular" files, as well as files in the /vsimem/ (read-write), /vsizip/ (read-only), /vsigzip/ (read-only), /vsicurl/ (read-only) domains.

Note: for regular files, the standard I/O operations provided by SQLite are used, in order to benefit from its integrity guarantees.

## Using the SpatiaLite library (Spatial extension for SQLite)

(Starting with GDAL 1.7.0)

The SQLite driver can read and write SpatiaLite databases. Creating or updating a spatialite database requires explicit linking against SpatiaLite library (version >= 2.3.1). Explicit linking against SpatiaLite library also provides access to functions provided by this library, such as spatial indexes, spatial functions, etc...

A few examples :

    # Duplicate the sample database provided with SpatiaLite
    ogr2ogr -f SQLite testspatialite.sqlite test-2.3.sqlite  -dsco SPATIALITE=YES

    # Make a request with a spatial filter. Will work faster if spatial index has
    # been created and explicit linking against SpatiaLite library.
    ogrinfo testspatialite.sqlite Towns -spat 754000 4692000 770000 4924000

## Opening with 'VirtualShape:'

(Require OGR >= 1.9.0 and Spatialite support)

It is possible to open on-the-fly a shapefile as a VirtualShape with Spatialite. The syntax to use for the datasource is "VirtualShape:/path/to/shapefile.shp" (the shapefile must be a "real" file).

This gives the capability to use the spatial operations of Spatialite (note that spatial indexes on virtual tables are not available).

## Creation Issues

The SQLite driver supports creating new SQLite database files, or adding tables to existing ones. Note that a new database file cannot be created over an existing file.

### Database Creation Options

- METADATA=yes/no: This can be used to avoid creating the geometry_columns and spatial_ref_sys tables in a new database. By default these metadata tables are created when a new database is created.
- SPATIALITE=yes/no: (Starting with GDAL 1.7.0) Create the SpatiaLite flavour of the metadata tables, which are a bit differ from the metadata used by this OGR driver and from OGC specifications. Implies METADATA=yes.
  Please note: (Starting with GDAL 1.9.0) OGR must be linked against libspatialite in order to support insert/write on SpatiaLite; if not, read-only mode is enforced.
  Attempting to perform any insert/write on SpatiaLite skipping the appropriate library support simply produces broken (corrupted) DB-files.
  Important notice: when the underlaying libspatialite is v.2.3.1 (or any previous version) any Geometry will be casted to 2D [XY], because earlier versions of this library are simply able to support 2D [XY] dimensions. Version 2.4.0 (or any subsequent) is required in order to support 2.5D [XYZ].
- INIT_WITH_EPSG=yes/no: (Starting with GDAL 1.8.0) Insert the content of the EPSG CSV files into the spatial_ref_sys table. Defaults to no.
  Please note: if SPATIALITE=yes and the underlaying libspatialite is v.2.4 (or any subsequent version) INIT_WITH_EPSG is ignored anyway; any recent version of this library will unconditionally load the EPSG dataset into the spatial_ref_sys table when creating a new DB (self-initialization).

### Layer Creation Options

- FORMAT=WKB/WKT/SPATIALITE: Controls the format used for the geometry column. By default WKB (Well Known Binary) is used. This is generally more space and processing efficient, but harder to inspect or use in simple applications than WKT (Well Known Text). SpatiaLite extension uses its own binary format to store geometries and you can choose it as well. It will be selected automatically when SpatiaLite database is opened or created with SPATIALITE=yes option. SPATIALITE value is available starting with GDAL 1.7.0
- LAUNDER=yes/no: Controls whether layer and field names will be laundered for easier use in SQLite. Laundered names will be convered to lower case and some special characters will be changed to underscores.
- SPATIAL_INDEX=yes/no: (Starting with GDAL 1.7.0) If the database is of the SpatiaLite flavour, and if OGR is linked against libspatialite, this option can be used to control if a spatial index must be created. Default to yes.
- COMPRESS_GEOM=yes/no: (Starting with GDAL 1.9.0) If the format of the geometry BLOB is of the SpatiaLite flavour, this option can be used to control if the compressed format for geometries (LINESTRINGs, POLYGONs) must be used. This format is understood by Spatialite v2.4 (or any subsequent version). Default to no. Note: when updating an existing Spatialite DB, the COMPRESS_GEOM configuration option can be set to produce similar results for appended/overwritten features.

## Performance hints

SQLite is a Transactional DBMS; while many INSERT statements are executed in close sequence, BEGIN TRANSACTION and COMMIT TRANSACTION statements have to be invoked appropriately in order to get optimal performance. The default OGR behavior is to COMMIT a transaction every 200 inserted rows. This value is surely too low for SQLite; and closing too much frequently the current transaction causes severe performance degradation. The -gt argument allows to explicitly set the number of rows for each transaction. Explicitly defining -gt 1024 usually ensures a noticeable performance boost; defining an even bigger -gt 65536 ensures optimal performance while populating some table containing many hundredth thousand or million rows.

SQLite usually has a very minimal memory foot-print; just about 20MB of RAM are reserved to store the internal Page Cache [merely 2000 pages]. This value too may well be inappropriate under many circumstances, most notably when accessing some really huge DB-file containing many tables related to a corresponding Spatial Index. Explicitly setting a much more generously dimensioned internal Page Cache may often help to get a noticeably better performance. Starting since GDAL 1.9.0 you can explicitly set the internal Page Cache size using the configuration option OGR_SQLITE_CACHE value [value being measured in MB]; if your HW has enough available RAM, defining a Cache size as big as 512MB (or even 1024MB) may sometimes help a lot in order to get better performance.

## Credits

- Development of the OGR SQLite driver was supported by DM Solutions Group and GoMOOS.
- Full support for SpatiaLite was contributed by A.Furieri, with funding from Regione Toscana

## Links

- http://www.sqlite.org: Main SQLite page.
- http://www.gaia-gis.it/spatialite/: SpatiaLite extension to SQLite.
- FDO RFC 16: FDO Provider for SQLite