0 branches
Tree Top files
Code
Clone with HTTPS:
56 years ago
..
pool.v db: connection pool (#24161) 1 year ago 1010 bytes

Description

sqlite is a thin wrapper for the SQLite library, which in turn is "a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine."

Install SQLite Dependency

On any platform (Windows, Linux, macOS), you can run:

v vlib/db/sqlite/install_thirdparty_sqlite.vsh

This downloads the SQLite amalgamation source and places it in v/thirdparty/sqlite. V will then compile it automatically during your build.

If you need to install SQLite manually, use the SQLite amalgamation package. v/thirdparty/sqlite must contain sqlite3.c and sqlite3.h directly.

Do not use the autoconf/source package here, and do not rename sqlite3.c to sqlite3.cpp.

On macOS, V can also fall back to the system libsqlite3 when pkg-config is not available, so an extra install is often not needed.

On Linux, you can also install the system development package instead:

Convenience Methods

The DB struct provides several helper methods for common introspection queries:

import db.sqlite

db := sqlite.connect('mydb.db') or { panic(err) }

// List all user tables
tables := db.tables()!

// Get column names for a table
cols := db.columns('users')!

// Get CREATE statements (single table or all objects)
s := db.schema('users')!

// Database file size in bytes
size := db.db_size()!

Interactive CLI

V includes a built-in SQLite CLI as a replacement for sqlite3:

v sqlite mydb.db

Features include a full readline REPL with history and tab completion, 9 output modes (table, box, markdown, csv, json, line, html, insert, quote), .dump, .import/.export, .backup, session control, and schema tools. Run .help inside the REPL for the full command list.

Performance Tips

When performing a large amount of database calls (i.e. INSERTS), significant performance increase can be obtained by controlling the synchronization and journal modes.

For instance:

import db.sqlite

db := sqlite.connect('foo.db') or { panic(err) }
db.synchronization_mode(sqlite.SyncMode.off)!
db.journal_mode(sqlite.JournalMode.memory)!