Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "swiplite"

Title:Prolog bindings for SQLite3
Rating:Not rated. Create the first rating!
Latest version:0.3
SHA1 sum:1ea2a5f73cdecab972c1ad811763db430a7ac202
Author:Boris Vassilev <boris.vassilev@gmail.com>

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.31ea2a5f73cdecab972c1ad811763db430a7ac2022https://github.com/borisvassilev/swiplite/archive/V0.3.zip
0.28be0cc945cf9cb4bef7c82f694e3d0e1f3d7ede13https://github.com/borisvassilev/swiplite/archive/0.2.zip
0.1.1b151e55053849d23a1eb2cb6ac28fca155f0076c3https://github.com/borisvassilev/swiplite/archive/0.1.1.zip
0.101eee77379899032f179f740c7548651a1b934083https://github.com/borisvassilev/swiplite/archive/0.1.zip

SQLite for SWI-Prolog

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.

Requirements

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.

Finally, since this package contains C code, you need CMake and a C compiler.

Installation

The pack has been already published. To get the latest release:

$ swipl pack install swiplite

... or from the top level:

?- pack_install(swiplite).

Installing from source

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

Embedding the SQLite source

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, then dropping the two files sqlite3.c and sqlite3.h in the `c/` subdirectory.

You can first check the diff:

$ git checkout sqlite3-amalgamation
Switched to branch 'sqlite3-amalgamation'
Your branch is up to date with 'origin/sqlite3-amalgamation'.

$ git diff main
diff --git a/CMakeLists.txt b/CMakeLists.txt

# Create the library as a CMake module
-add_library(swiplite MODULE c/swiplite.c)
+add_library(swiplite MODULE c/swiplite.c c/sqlite3.c)

-find_package(SQLite3 REQUIRED)
-target_link_libraries(swiplite PRIVATE ${SQLite3_LIBRARIES})
-target_include_directories(swiplite PRIVATE ${SQLite3_INCLUDE_DIRS})

diff --git a/c/swiplite.c b/c/swiplite.c

 #include <SWI-Prolog.h>
 #include <SWI-Stream.h>
-#include <sqlite3.h>
+#include "sqlite3.h"

Once you add the two files you will find in the amalgamation 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.

Smoke test

To check that you have installed the add-on correctly:

?- use_module(library(sqlite)).
true.

?- sqlite_version(V).
V = '3.47.2'.

The predicate sqlite_version/1 opens an in-memory SQLite database and queries "`Select sqlite_version()`".

Background

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.

Why SQL?

There is no good reason to use SQL; other than, it is the standard interface to relational databases.

Why a relational database?

... especially since you are using Prolog already?

Relational databases come with features that are not available out of the box on SWI-Prolog:

  • Foreign key, unique, and check constraints;
  • Disk persistency.

Why SQLite?

SQLite provides a full-featured relational database almost for free, in terms of setup and resources needed.

Why not ODBC?

SWI-Prolog provides a full-features ODBC interface). However, specifically in the case of SQLite, it requires a clumsy setup with extra dependencies and no obvious benefit.

Why not proSQLite?

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.

Why do you need prepared statements?

It is very likely that prepared statements are not that important. On the other hand, there is the cautionary tale of Little Bobby Tables.

Contents of pack "swiplite"

Pack contains 7 files holding a total of 67.0K bytes.