Modern Fortran bindings to SQLite3, a fast, embedded SQL database engine.
- Low-level module
sqlite3_c: direct, 1:1bind(c)interfaces to the SQLite3 C API. - High-level module
sqlite3: object-oriented, memory-safe wrapper (sqlite3_db_t,sqlite3_stmt_t) with automatic string conversion.
- A Fortran compiler supporting the Fortran 2008 standard or later (gfortran, ifx, nvfortran, flang, ...).
- SQLite3 development headers and library (
libsqlite3-devon Debian/Ubuntu). - fpm or CMake (>= 3.16) to build and test the package.
Add this package as a git dependency in your fpm.toml:
[dependencies]
fortran-sqlite3 = { git = "https://github.com/AnmiTaliDev/fortran-sqlite3.git" }Alternatively, clone the repository and build/install it with CMake:
git clone https://github.com/AnmiTaliDev/fortran-sqlite3.git
cmake -S fortran-sqlite3 -B build -DCMAKE_INSTALL_PREFIX=/usr/local
cmake --build build
cmake --install buildThis installs the static library, the compiled .mod files, and a
fortran-sqlite3.pc pkg-config file, so downstream projects can pick up the
compiler and linker flags with:
gfortran my_program.f90 -o my_program $(pkg-config --cflags --libs fortran-sqlite3)You can also add it as a subdirectory or via FetchContent/find_package
in another CMake project and link against the
fortran-sqlite3::fortran-sqlite3 target.
With fpm:
fpm build
fpm testWith CMake:
cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failureThe test suite is built by default when CMake is configured directly on this
repository. Pass -DBUILD_TESTING=OFF to skip it (it is already off by
default when this project is pulled in as a CMake subdirectory of another
project).
program example
use, intrinsic :: iso_c_binding
use sqlite3
implicit none
type(sqlite3_db_t) :: db
type(sqlite3_stmt_t) :: stmt
integer(c_int) :: rc
rc = db%open("example.db")
rc = db%exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT);")
rc = db%prepare("INSERT INTO users (name) VALUES (?);", stmt)
rc = stmt%bind_text(1, "Alice")
rc = stmt%step()
rc = stmt%finalize()
rc = db%prepare("SELECT id, name FROM users;", stmt)
do while (stmt%step() == SQLITE_ROW)
print *, stmt%column_int(0), trim(stmt%column_text(1))
end do
rc = stmt%finalize()
rc = db%close()
end program example| Procedure | Description |
|---|---|
open(filename, [flags]) |
Open a database connection |
close() |
Close the connection |
exec(sql) |
Execute SQL directly (no result rows) |
prepare(sql, stmt) |
Compile SQL into a sqlite3_stmt_t |
errmsg() |
Last error message |
errcode() |
Last error code |
last_insert_rowid() |
Rowid of the last insert |
changes() |
Rows affected by the last statement |
is_open() |
Whether the connection is open |
| Procedure | Description |
|---|---|
step() |
Advance to the next row / execute |
reset() |
Reset for re-execution |
finalize() |
Destroy the statement |
clear_bindings() |
Clear all parameter bindings |
bind_int/int64/double/text/null(idx, value) |
Bind a parameter (1-based index) |
column_count() |
Number of result columns |
column_type(icol) |
Storage class of a column (0-based) |
column_name(icol) |
Name of a column (0-based) |
column_int/int64/double/text(icol) |
Read a column value (0-based) |
is_valid() |
Whether the statement handle is valid |
This project is released under the CC0 1.0 Universal public domain dedication. See LICENSE for full text.
AnmiTaliDev anmitalidev@nuros.org