proSQLite: a Prolog interface to the SQLite database system.
This library follows the design and borrows code from the
ODBC
library of SWI-Prolog
The SQLite system is a powerful zero-configuration management systme that interacts
with single-file databases that are cross-platform compatible binaries.
ProSQLite provides three layers of interaction with SQLite databases.
At the lower level is the querying via SQL statements. A second layer
allows the interogation of the database dictionary, and the final level
facilitates the viewing of database tables as predicates.
See the publications pointed to by sqlite_citation/2, for further details.
If you use prosqlite in your research, please consider citing these publication.
The library has been developed and tested on SWI 6.3.2 but it should
also work on YAP Prolog.
The easiest way to install on SWI is via the package manager.
If you can compile C-code, then simply do
?- pack_install( prosqlite ).
And you are good to go.
There are good/full examples in the sources, directory examples/.
For instance test by :
?- [predicated].
?- predicated.
There is a sister package, db_facts (also installable via the manager).
Db_facts, allow interaction with the underlying database via Prolog terms,
That library can also be used as a common compatibility layer for the ODBC
and proSQLite libraries of SWI-Prolog, as it works on both type of connections.
ProSQLite is debug/1 aware: call debug(sqlite)
to see what is sent to
the sqlite engine.
There are MS wins DLLs included in the sources. However the package manager of SWI 6.3.2
cannot currently cope with these. The DLLs are at the correct location so with any luck the
next version of the manager will install these correctly.
In the meantime, take the sources from
http://bioinformatics.nki.nl/~nicos/sware/prosqlite/prosqlite-0.1.0.tgz
or
http://bioinformatics.nki.nl/~nicos/sware/prosqlite/prosqlite-0.1.0.zip
and
if you run a 32 bit prolog do,
- copy the dll files from lib/i386-win32/ to swipl/bin/
- and prolog file prolog/prosqlite.pl to swipl/library/
If you are running a 64 bit SWI executable do:
- copy the dll files from lib/i386-win32/ to swipl/bin/
- and prolog file prolog/prosqlite.pl to swipl/library/
That's it.
- author
- - Nicos Angelopoulos
- - Sander Canisius
- version
- - 0.1.0, 2012/10/17
- See also
- - Sander Canisius, Nicos Angelopoulos and Lodewyk Wessels. Exploring file based databases via an Sqlite interface. In the ICLP Workshop on Logic-based methods in Programming Environments, p. 2-9, (2012, Budapest, Hungary).
- - http://bioinformatics.nki.nl/~nicos/pbs/wlpe2012_sqlite.pdf
- - http://bioinformatics.nki.nl/~nicos/sware/prosqlite
- - http://bioinformatics.nki.nl/~nicos/sware/db_facts
- - http://www.sqlite.org/
- - files in examples/ directory
- - also available as a SWI pack http://www.swi-prolog.org/pack/list
- - sources at http://bioinformatics.nki.nl/~nicos/sware/prosqlite/prosqlite-0.1.0.tgz
- license
- - Perl Artistic License
- To be done
- - set pragmas
-
sqlite_version
(-Version, -Date)
- The current version. Version is a Mj:Mn:Fx term, and date is a date(Y,M,D) term.
-
sqlite_binary_version
(-Version, -Date)
- The current version of the binaries. If the installed binaries are not compiled from
the sources, then this might be different (=older) that the sqlite Porlog source version
returned by sqlite_version/2. Version is a Mj:Mn:Fx term, and date is a date(Y,M,D) term.
-
sqlite_citation
(-Atom, -Bibterm)
- Succeeds once for each publication related to this library. Atom is the atom representation
suitable for printing while Bibterm is a bibtex(Type,Key,Pairs) term of the same publication.
Produces all related publications on backtracking.
-
sqlite_connect
(+File, ?Alias)
- Open a connection to an sqlite File. If Alias is a variable, an opaque atom
is generated and unified to it. The opened db connection to file can be accessed via Alias.
sqlite_connect('uniprot.sqlite', uniprot).
-
sqlite_connect
(+File, ?Connection, +Options)
- Open a connection to an sqlite File. If Connection is a variable, an opaque atom
is generated, otherwise the opened file is connected to handle Connecction.
Options is a sinlge term or a list of terms from the following:
- alias(Atom) identify the connection as Alias in all transactions
- as_predicates(AsPred) if true, create hook predicates that map
each sqlite table to a prolog predicate.
These are created in module user, and it is
the user's responsibility to be unique in this module.
- at_module(AtMod) the module at which the predicates will be asserted at
(if as_predicates(true)) is also given). Default is
user
.
- table_as(Table,Pname,Arity) map the table to predicate with name Pname. Arity should be
defined for this representaition as per with_arity() option.
- arity(arity) Arity denotes the arity of access clauses to be added in the prolog database that
correspond to SQLite tables. The default is
arity
, which asserts a
predicate matching the arity of the table.
both
adds two predicates, one matching the arity and a single argument one.
The later can be interrogated with something like
?- phones( [name=naku, telephone=T] ).
unary
only adds the unary version, and palette
adds a suite of predicates
with arities from 1 to N, where N is the number of columns.
These can be interrogated by :
?- phones( name=Name ).
?- phones( name=naku, telephone=T ).
?- phones( [name=naku, telephone=T] ).
Predicated tables can be used to insert values to the database by virtue of all
their columns are give ground values.
- exists(Boolean) do not throw an error if file does not exist and
Boolean is false. Default is true and an error is
thrown if the Sqlite file does not exist.
- ext(Ext) database files are assumed to have an sqlite extension at their end.
To ovewrite this give Ext ('' for no extension).
When unary predicates are defined the columns can be interrogated/accessed by list pairs of the form Column=Value.
Column-Value and Column:Value are also recognised.
So for example, for table phones
with columns Name, Address and Phone, prosqlite will add
phones(_,_,_)
as a response to as_predicates, and
phones(_)
if Arity is unary
The latter can be interrogated by
phones( ['Name'=naku','Phone'=Phone] ).
which will return the phone number(s) associated with individual named by naku
.
See source file examples/predicated.pl .
-
sqlite_disconnect
(+Alias)
- Terminate the connection to a SQLite database file.
sqlite_disconnect(uniprot).
-
sqlite_current_connection
(-Connection)
- Return or interrogate the name of open connection handles.
-
sqlite_default_connection
(-Connection)
- Return or interrogate the name of the default connection. This is the
last connection opened.
-
sqlite_query
(+Sql, -Row)
- Post an Sql query to default connection and get row result in Row.
-
sqlite_query
(+Connection, +Sql, -Row)
- Post an Sql query to Sqlite Connection and get row result in Row.
-
sqlite_format_query
(+Connection, +FAs, -Row)
- Post a format style Sql query to Sqlite Connection and get row result in Row.
FAs is a - pair structure : Format-Arguments.
sqlite_format_query(uniprot, 'PRAGMA table_info(~w)'-Table, row(_, Column, _, _, _, _))
-
sqlite_current_table
(+Connection, -Table)
- Return or interrogate tables in the Sqlite database associated with Connection.
-
sqlite_current_table
(+Connection, ?Table, -Facet)
- Facet is a property of Table found at Connection. Currently only arity(Arity) is
delivered.
-
sqlite_table_column
(+Connection, ?Table, -Column)
- Return or interrogate tables and columns in the Sqlite database associated with Connection.
-
sqlite_table_column
(+Connection, ?Table, ?Column, -Facet)
- Facet is one of:
- position(Nth0) position of the Column in the table, first being 0.
- data_type(Dtype) the data type for the column
- nullable(Null) can this column be set to the null value
- defautl(Default) the default value for the
- primary_key(Key) is this column part of the primary key ?
-
sqlite_pragma
(+Alias, +Pragma, -Row)
- Interrogate SQLite Pragmas. Currently only reading is supported.
Pragma can be an atom or a - separated pair, as in
table_info-TableName
.
sqlite_pragma( phone_db, encoding, Row).
-
sqlite_table_count
(+Connection, +Table, -Count)
- True if Count is the number of rows in Sqlite Connection associated Table.
-
sqlite_date_sql_atom
(Date, Sql)
- Convert between a Prolog date/3 term and an Sql atom.
The conversion is bidirectional.