Search results

There are no results.

sqlite

Low-level bindings to SQLite.

This module provides a set of bindings to the SQLite C API. For more information, refer to the documentation of the Database type.

Requirements

The bindings provided by this module use the SQLite C API and require that a SQLite shared or static library is available to link against. For example, on Fedora you'll need to install the sqlite-devel package.

Examples

Creating a database connection and inserting some data:

import sqlite (Database)

let db = Database.new(':memory:').or_panic

let _ = db.execute('CREATE TABLE users (name TEXT)').or_panic
let _ = db.execute('INSERT INTO users VALUES ("Alice")').or_panic

Doing the same but by binding runtime values:

import sqlite (Database)

let db = Database.new(':memory:').or_panic

let _ = db.execute('CREATE TABLE users (name TEXT)').or_panic
let st = db.prepare('INSERT INTO users VALUES (?)').or_panic

st.bind(column: 0, value: 'Alice').or_panic
st.execute.or_panic

Creating a BLOB value by incrementally writing data to it:

import sqlite (Database, MAIN_DB)

let db = Database.new(':memory:').or_panic
let _ = db.execute('CREATE TABLE files (name TEXT, data BLOB)').or_panic
let st = db.prepare('INSERT INTO files VALUES (:name, :data)').or_panic
let val = 'This is an example'

st.bind(':name', 'README.md').or_panic
st.bind_blob(':data', val.size).or_panic

let _ = st.execute.or_panic
let id = db.last_inserted_row_id
let blob = db
  .blob(MAIN_DB, table: 'files', column: 'data', row: id, write: true)
  .or_panic

blob.write(val).or_panic

For small blobs it's usually easier/less verbose to use Statement.bind with a ByteArray, but for large blobs you'll want to use the incremental blob API to avoid having to load entire blobs into memory.

Constants

MAIN_DB

The name of the main database.

Traits

BindIndex

A column index or name to bind to a SQLite statement.

BindValue

A value that may be bound to a column in a SQLite statement.

Types

Blob

A handle to a SQLite BLOB value.

Database

A SQLite database connection.

ValueError

An error produced by SQLite.

Row

A single row produced by the Rows iterator.

Rows

An iterator over the rows produced by a prepared statement.

Statement

A prepared SQL statement to execute.