| Did you know ... | Search Documentation: |
| Packs (add-ons) for SWI-Prolog |
| Title: | Prolog bindings for SQLite3 |
|---|---|
| Rating: | Not rated. Create the first rating! |
| Latest version: | 1.1 |
| SHA1 sum: | 398dea3a572f0bb0ef1b0a92f06db3854afd9fe6 |
| Author: | Boris Vassilev <boris.vassilev@gmail.com> |
No reviews. Create the first review!.
| Version | SHA1 | #Downloads | URL |
|---|---|---|---|
| 1.1 | 398dea3a572f0bb0ef1b0a92f06db3854afd9fe6 | 3 | https://github.com/borisvassilev/swiplite/archive/V1.1.zip |
| 0.3 | 1ea2a5f73cdecab972c1ad811763db430a7ac202 | 21 | https://github.com/borisvassilev/swiplite/archive/V0.3.zip |
| 0.2 | 8be0cc945cf9cb4bef7c82f694e3d0e1f3d7ede1 | 3 | https://github.com/borisvassilev/swiplite/archive/0.2.zip |
| 0.1.1 | b151e55053849d23a1eb2cb6ac28fca155f0076c | 3 | https://github.com/borisvassilev/swiplite/archive/0.1.1.zip |
| 0.1 | 01eee77379899032f179f740c7548651a1b93408 | 3 | https://github.com/borisvassilev/swiplite/archive/0.1.zip |
The purpose of swiplite is to enable using sqlite3 databases comfortably from SWI-Prolog, without sacrificing efficiency.
The prolog/sqlite.pl provides low-level access to the connection and statement objects.
In addition to this document, there is a discussion on the SQLITEDOC.md in comparison to other relational databases. There is DEVELOPMENT.md summarizing information about the development process of the library.
This has been developed and tested on Linux and Mac OS.
You might already have a decently recent version of SQLite installed; you can also install it using the package manager for your OS. On MacOS, it is strongly recommended to install SQLite from Homebrew and make sure this library is linked against it. The default SQLite that comes with MacOS is somewhat out of date and seems to be compiled with questionable flags, probably for backwards compatibility.
Finally, since this package contains C code, you need CMake and a C compiler.
The pack has been already published. To get the latest release:
$ swipl pack install swiplite
... or from the top level:
?- pack_install(swiplite).
The code is available in the public GitHub repository https://github.com/borisvassilev/swiplite. To install from the source, clone the repo and install from the pack directory:
$ git clone https://github.com/borisvassilev/swiplite.git $ cd swiplite $ swipl pack install .
Alternatively, create an archive and install it locally:
$ git archive --output=swiplite-<version>.tgz <tree-ish> $ swipl pack install swiplite-<version>.tgz
If you prefer to use the SQLite source provided on
SQLite's download page,
you can do it by first checking out its own branch,
sqlite3-amalgamation.
Once you add the two files you will find in the
amalgamation,
sqlite3.c and sqlite3.h, in the `c/` subdirectory,
you should see:
$ git status
On branch sqlite3-amalgamation
Your branch is up to date with 'origin/sqlite3-amalgamation'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
c/sqlite3.c
c/sqlite3.h
nothing added to commit but untracked files present (use "git add" to track)
You can now build and install the library from the local repo as described above.
To check that you have installed the add-on correctly:
?- use_module(library(sqlite)). true. ?- sqlite_version(V). V = '3.53.0'.
The predicate sqlite_version/1 opens an in-memory SQLite
database and queries `Select sqlite_version()`.
There are many alternatives to using the swiplite pack.
Here is a list of questions I have asked myself before
I started working on it (and repeatedly ever since), along
with attempts at answers.
There is no good reason to use SQL; other than, it is the standard interface to relational databases.
... especially since you are using Prolog already?
Relational databases come with features that are not available out of the box on SWI-Prolog:
SQLite provides a full-featured relational database almost for free, in terms of setup and resources needed.
SWI-Prolog provides a full-featured ODBC interface. However, specifically in the case of SQLite, it requires a clumsy setup with extra dependencies and no obvious benefit.
proSQLite is a mature SWI-Prolog interface to SQLite also available as a pack. Unfortunately, I was not able to figure out how to use it to create prepared statements with SQL parameters and bind values to them.
It is very likely that prepared statements are not that important. On the other hand, there is the cautionary tale of Little Bobby Tables.
I have described one bug and feature in SQLITEDOC.md, in "Using a literal in an SQL statement": a Prolog integer that does not fit in an 8-byte signed integer will be silently converted to an SQLite REAL, and the original value will be irreversibly lost. In contrast, if you use a prepared statement as provided by this library, swiplite, and you attempt to bind an integer that does not fit, you will get a run-time error.
This library has been used by me for about a year at this point of time (2026-04-25) and it has served me well, for the very limited purposes I have had. A couple of features I would sometimes wish I would have had:
In addition, the CMakeLists.txt currently provided in this library happens to work for the two machines I have in daily use. In the unlikely case that someone else wants to use this library they might be able to propose improvements.
Pack contains 8 files holding a total of 102K bytes.