GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing docs.sqlitecloud.io — SQLite-Memory API Reference | SQLite Cloud Docs SQLite-Memory API ReferenceA SQLite extension that provides semantic memory capabilities with hybrid search (vector similarity + full-text search). Table of Contents
Overviewsqlite-memory enables semantic search over text content stored in SQLite. It:
Sync BehaviorAll Change Detection
Transactional SafetyEvery sync operation is wrapped in a SQLite SAVEPOINT transaction. If any step fails (embedding error, disk issue, constraint violation), the entire operation rolls back. This guarantees:
This makes all sync functions idempotent and safe to call repeatedly (e.g., on a schedule or at application startup). SQL FunctionsGeneral Functions
|
| Parameter | Type | Description |
|---|---|---|
provider | TEXT | "local" for built-in llama.cpp engine, or any other name (e.g., "openai") for vectors.space remote service |
model | TEXT | For local: full path to GGUF model file. For remote: model identifier supported by vectors.space |
Returns: INTEGER - 1 on success
Notes:
provider is "local", the extension uses the built-in llama.cpp engine and verifies the model file existsprovider is anything other than "local", the extension uses the vectors.space remote embedding servicememory_set_apikey)dbmem_settings tableExample:
-- Local embedding model (uses built-in llama.cpp engine)
SELECT memory_set_model('local', '/path/to/nomic-embed-text-v1.5.Q8_0.gguf');
-- Remote embedding via vectors.space (requires free API key)
SELECT memory_set_model('openai', 'text-embedding-3-small');
SELECT memory_set_apikey('your-vectorspace-api-key');
memory_set_apikey(key TEXT)Sets the API key for the vectors.space remote embedding service.
Parameters:
| Parameter | Type | Description |
|---|---|---|
key | TEXT | API key obtained from vectors.space (free account) |
Returns: INTEGER - 1 on success
Notes:
"local"Example:
SELECT memory_set_apikey('your-vectorspace-api-key');
memory_set_option(key TEXT, value ANY)Sets a configuration option.
Parameters:
| Parameter | Type | Description |
|---|---|---|
key | TEXT | Option name (see Configuration Options) |
value | ANY | Option value (type depends on the option) |
Returns: INTEGER - 1 on success
Example:
-- Set maximum tokens per chunk
SELECT memory_set_option('max_tokens', 512);
-- Enable engine warmup
SELECT memory_set_option('engine_warmup', 1);
-- Set minimum score threshold
SELECT memory_set_option('min_score', 0.75);
memory_get_option(key TEXT)Retrieves a configuration option value.
Parameters:
| Parameter | Type | Description |
|---|---|---|
key | TEXT | Option name |
Returns: ANY - Option value, or NULL if not set
Example:
SELECT memory_get_option('max_tokens');
-- Returns: 400
SELECT memory_get_option('provider');
-- Returns: "local"
memory_add_text(content TEXT [, context TEXT])Syncs text content to memory. Duplicate content (same hash) is skipped automatically.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
content | TEXT | Yes | Text content to store and index |
context | TEXT | No | Optional context label for grouping memories |
Returns: INTEGER - 1 on success
Notes:
max_tokens and overlay_tokens settingsdbmem_vaultcreated_at timestamp automaticallyExample:
-- Add text without context
SELECT memory_add_text('SQLite is a C-language library that implements a small, fast, self-contained SQL database engine.');
-- Add text with context
SELECT memory_add_text('Important meeting notes from 2024-01-15...', 'meetings');
memory_add_file(path TEXT [, context TEXT])Syncs a file to memory. Unchanged files are skipped; modified files are atomically replaced.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
path | TEXT | Yes | Full path to the file |
context | TEXT | No | Optional context label for grouping memories |
Returns: INTEGER - 1 on success
Notes:
md,mdx)dbmem_content.pathDBMEM_OMIT_IOExample:
SELECT memory_add_file('/docs/readme.md');
SELECT memory_add_file('/docs/api.md', 'documentation');
memory_add_directory(path TEXT [, context TEXT])Synchronizes a directory with memory. Adds new files, reindexes modified files, and removes entries for deleted files.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
path | TEXT | Yes | Full path to the directory |
context | TEXT | No | Optional context label applied to all files |
Returns: INTEGER - Number of new files processed
Notes:
DBMEM_OMIT_IOExample:
SELECT memory_add_directory('/path/to/docs');
-- Returns: 42 (number of new files processed)
SELECT memory_add_directory('/project/notes', 'project-notes');
-- Safe to call again — unchanged files are skipped
SELECT memory_add_directory('/path/to/docs');
-- Returns: 0 (nothing changed)
memory_delete(hash INTEGER)Deletes a specific memory by its hash.
Parameters:
| Parameter | Type | Description |
|---|---|---|
hash | INTEGER | The hash identifier of the memory to delete |
Returns: INTEGER - Number of content entries deleted (0 or 1)
Notes:
dbmem_content, dbmem_vault, and dbmem_vault_ftsdbmem_content table or search resultsExample:
-- Get hash from content table
SELECT hash FROM dbmem_content WHERE path LIKE '%readme%';
-- Delete by hash
SELECT memory_delete(1234567890);
memory_delete_context(context TEXT)Deletes all memories with a specific context.
Parameters:
| Parameter | Type | Description |
|---|---|---|
context | TEXT | The context label to match |
Returns: INTEGER - Number of content entries deleted
Notes:
context matches exactlyExample:
-- Delete all memories with context 'meetings'
SELECT memory_delete_context('meetings');
-- Returns: 15
memory_clear()Deletes all memories from the database.
Parameters: None
Returns: INTEGER - 1 on success
Notes:
dbmem_content, dbmem_vault, and dbmem_vault_ftsdbmem_settingsdbmem_cache)Example:
SELECT memory_clear();
memory_cache_clear([provider TEXT, model TEXT])Clears the embedding cache.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
provider | TEXT | No | Provider name to clear cache for |
model | TEXT | No | Model name to clear cache for |
Returns: INTEGER - Number of cache entries deleted
Notes:
Example:
-- Clear entire cache
SELECT memory_cache_clear();
-- Clear cache for a specific provider/model
SELECT memory_cache_clear('openai', 'text-embedding-3-small');
Require sqlite-sync to be loaded before use.
memory_enable_sync([context TEXT, ...])Enables CRDT-based synchronization for dbmem_content via sqlite-sync. Uses the CLS algorithm with block-level LWW on the value column for fine-grained conflict resolution.
Parameters: Zero or more TEXT context names. If no arguments are given, all memory is synced. If one or more context names are provided, only rows matching those contexts are synced.
Returns: INTEGER - 1 on success
Notes:
value enables line-level conflict resolution for text contentExample:
-- Sync all memory
SELECT memory_enable_sync();
-- Sync only specific contexts
SELECT memory_enable_sync('conversation', 'project-docs');
memory_disable_sync()Removes synchronization infrastructure from dbmem_content, disabling all replication. The table data is preserved.
Parameters: None
Returns: INTEGER - 1 on success
Notes:
Example:
SELECT memory_disable_sync();
memory_searchA virtual table for performing hybrid semantic search.
Query Format:
SELECT * FROM memory_search WHERE query = 'search text';
Hidden filter columns (used in WHERE):
| Column | Type | Required | Description |
|---|---|---|---|
query | TEXT | Yes | The search query |
max_entries | INTEGER | No | Override max_results setting for this query only |
context | TEXT | No | Restrict results to a specific context label |
Output columns:
| Column | Type | Description |
|---|---|---|
hash | INTEGER | Content hash identifier |
seq | INTEGER | Chunk sequence number within the document (0-based) |
ranking | REAL | Combined similarity score (0.0 - 1.0) |
path | TEXT | Source file path or generated UUID for text content |
snippet | TEXT | Text snippet from the matching chunk |
Notes:
max_results setting (default: 20), overridable per-query with max_entriesmin_score setting (default: 0.7)last_accessed timestamp if update_access is enabledExample:
-- Basic search
SELECT path, snippet, ranking FROM memory_search WHERE query = 'database indexing strategies';
-- Search with ranking filter
SELECT path, snippet, ranking
FROM memory_search
WHERE query = 'how to optimize queries'
AND ranking > 0.8;
-- Restrict to a specific context
SELECT path, snippet, ranking
FROM memory_search
WHERE query = 'meeting action items'
AND context = 'meetings';
-- Override result limit for this query only
SELECT path, snippet, ranking
FROM memory_search
WHERE query = 'architecture overview'
AND max_entries = 5;
-- Get the chunk sequence number (useful for reconstructing document order)
SELECT path, seq, snippet, ranking
FROM memory_search
WHERE query = 'setup steps';
In addition to the SQL interface, sqlite-memory exposes a C API for embedding custom providers directly from application code.
sqlite3_memory_register_providerint sqlite3_memory_register_provider(
sqlite3 *db,
const char *provider_name,
const dbmem_provider_t *provider
);
Registers a custom embedding engine for a specific database connection. Once registered, calling memory_set_model(provider_name, model) from SQL will use your engine instead of the built-in local or remote engines.
Parameters:
| Parameter | Type | Description |
|---|---|---|
db | sqlite3 * | The database connection to register the provider on |
provider_name | const char * | Name used to activate the provider via memory_set_model() |
provider | const dbmem_provider_t * | Pointer to a struct containing the engine callbacks |
Returns: SQLITE_OK on success, or a SQLite error code.
dbmem_provider_t struct:
typedef struct {
// Called when memory_set_model(provider_name, model) is executed.
// api_key is the value set via memory_set_apikey() (may be NULL).
// xdata is the user pointer from this struct.
// Return an opaque engine pointer on success, or NULL on error (fill err_msg).
void *(*init)(const char *model, const char *api_key, void *xdata, char err_msg[1024]);
// Compute the embedding for the given text.
// Return 0 on success, non-zero on error.
int (*compute)(void *engine, const char *text, int text_len, void *xdata, dbmem_embedding_result_t *result);
// Free the engine. Called on context teardown or when the model changes.
// May be NULL if no cleanup is needed.
void (*free)(void *engine, void *xdata);
// Optional user-supplied pointer passed to all three callbacks.
void *xdata;
} dbmem_provider_t;
dbmem_embedding_result_t struct:
typedef struct {
int n_tokens; // Number of processed tokens (0 if unknown)
bool truncated; // True when the input was truncated before embedding
int n_embd; // Embedding dimension
float *embedding; // Embedding vector (engine-owned, valid until next call or free)
} dbmem_embedding_result_t;
Notes:
DBMEM_OMIT_LOCAL_ENGINE / DBMEM_OMIT_REMOTE_ENGINE compile flagsembedding buffer in dbmem_embedding_result_t must remain valid until the next compute call or free — it is engine-owned, not copied by the callern_tokens is metadata about the processed input when the engine can provide it; truncated is a boolean flag, not a truncated-token countExample:
#include "sqlite-memory.h"
typedef struct { int dimension; } MyEngine;
static void *my_init(const char *model, const char *api_key, void *xdata, char err_msg[1024]) {
MyEngine *e = malloc(sizeof(MyEngine));
e->dimension = 384;
return e;
}
static int my_compute(void *engine, const char *text, int text_len, void *xdata,
dbmem_embedding_result_t *result) {
MyEngine *e = (MyEngine *)engine;
static float vec[384];
// ... fill vec with your embedding ...
result->n_embd = e->dimension;
result->n_tokens = text_len / 4;
result->truncated = false;
result->embedding = vec;
return 0;
}
static void my_free(void *engine, void *xdata) {
free(engine);
}
// Register before using the database
dbmem_provider_t provider = {
.init = my_init,
.compute = my_compute,
.free = my_free,
.xdata = NULL,
};
sqlite3_memory_register_provider(db, "my-engine", &provider);
// Then from SQL:
// SELECT memory_set_model('my-engine', 'my-model-name');
// SELECT memory_add_text('some text to embed');
| Option | Type | Default | Description |
|---|---|---|---|
provider | TEXT | - | Embedding provider ("local" for llama.cpp, otherwise vectors.space) |
model | TEXT | - | Model path (local) or identifier (remote) |
dimension | INTEGER | - | Embedding dimension (auto-detected) |
max_tokens | INTEGER | 400 | Maximum tokens per chunk |
overlay_tokens | INTEGER | 80 | Token overlap between consecutive chunks |
chars_per_tokens | INTEGER | 4 | Estimated characters per token |
save_content | INTEGER | 1 | Store original content (1=yes, 0=no) |
skip_semantic | INTEGER | 0 | Skip markdown parsing, treat as raw text |
skip_html | INTEGER | 1 | Strip HTML tags when parsing |
extensions | TEXT | ”md,mdx” | Comma-separated file extensions to process |
engine_warmup | INTEGER | 0 | Warm up engine on model load (compiles GPU shaders) |
max_results | INTEGER | 20 | Maximum search results |
fts_enabled | INTEGER | 1 | Enable FTS5 in hybrid search |
vector_weight | REAL | 0.5 | Weight for vector similarity in scoring |
text_weight | REAL | 0.5 | Weight for FTS in scoring |
min_score | REAL | 0.7 | Minimum score threshold for results |
update_access | INTEGER | 1 | Update last_accessed on search |
embedding_cache | INTEGER | 1 | Cache embeddings to avoid redundant computation |
cache_max_entries | INTEGER | 0 | Max cache entries (0 = no limit). When exceeded, oldest entries are evicted |
search_oversample | INTEGER | 0 | Search oversampling multiplier (0 = no oversampling). When set, retrieves N * multiplier candidates from each index before merging down to N final results |
The extension tracks two timestamps for each memory:
created_atmemory_add_text, memory_add_file, or memory_add_directorylast_accessedupdate_access=1)update_access to 0Displaying timestamps in local time:
SELECT
path,
datetime(created_at, 'unixepoch', 'localtime') as created,
datetime(last_accessed, 'unixepoch', 'localtime') as accessed
FROM dbmem_content;
-- Check version
SELECT memory_version();
-- Configure local embedding model
SELECT memory_set_model('local', '/models/nomic-embed-text-v1.5.Q8_0.gguf');
-- Configure options
SELECT memory_set_option('max_tokens', 512);
SELECT memory_set_option('min_score', 0.75);
-- Add content
SELECT memory_add_text('SQLite is a C library that provides a lightweight disk-based database.', 'sqlite-docs');
SELECT memory_add_directory('/docs/sqlite', 'sqlite-docs');
-- Search
SELECT path, snippet, ranking
FROM memory_search
WHERE query = 'how does SQLite store data on disk';
-- View all memories with timestamps
SELECT
hash,
path,
context,
datetime(created_at, 'unixepoch', 'localtime') as created,
datetime(last_accessed, 'unixepoch', 'localtime') as last_used
FROM dbmem_content
ORDER BY last_accessed DESC;
-- Delete by context
SELECT memory_delete_context('old-docs');
-- Clear all
SELECT memory_clear();
-- Add memories with different contexts
SELECT memory_add_text('Meeting notes...', 'meetings');
SELECT memory_add_text('API documentation...', 'api-docs');
SELECT memory_add_text('Tutorial content...', 'tutorials');
-- Search within a context
SELECT * FROM memory_search
WHERE query = 'authentication'
AND context = 'api-docs';
-- List all contexts
SELECT context, COUNT(*) as count
FROM dbmem_content
GROUP BY context;
-- Delete a context
SELECT memory_delete_context('old-meetings');
-- Total memories and chunks
SELECT
(SELECT COUNT(*) FROM dbmem_content) as total_memories,
(SELECT COUNT(*) FROM dbmem_vault) as total_chunks;
-- Storage usage
SELECT
SUM(length(embedding)) as embedding_bytes,
SUM(length) as content_bytes
FROM dbmem_vault;
-- Memories by context
SELECT
COALESCE(context, '(none)') as context,
COUNT(*) as count
FROM dbmem_content
GROUP BY context;
-- Recently accessed
SELECT path, datetime(last_accessed, 'unixepoch', 'localtime') as last_used
FROM dbmem_content
WHERE last_accessed > 0
ORDER BY last_accessed DESC
LIMIT 10;
-- Tokens consumed and truncation per context
-- (n_tokens / truncated were added in schema version 2)
SELECT
COALESCE(c.context, '(none)') as context,
SUM(v.n_tokens) as tokens_processed,
SUM(v.truncated) as truncated_chunks
FROM dbmem_vault v
JOIN dbmem_content c ON c.hash = v.hash
GROUP BY c.context;
-- Chunks that the embedding model truncated on input
SELECT hash, seq, length, n_tokens
FROM dbmem_vault
WHERE truncated = 1;
| Option | Description |
|---|---|
DBMEM_OMIT_IO | Omit file/directory functions (for WASM) |
DBMEM_OMIT_LOCAL_ENGINE | Omit llama.cpp local engine (for remote-only builds) |
DBMEM_OMIT_REMOTE_ENGINE | Omit vectors.space remote engine (for local-only builds) |
SQLITE_CORE | Compile as part of SQLite core (not as loadable extension) |
All functions return an error if:
Errors can be caught using standard SQLite error handling mechanisms.
-- Example error handling in application code
SELECT memory_add_text(123); -- Error: expects TEXT parameter
SELECT memory_delete('abc'); -- Error: expects INTEGER parameter