ORM
(This is still in an alpha state)
Deprecation notice: the Function Call API (
orm_fn;orm.new_query[T]/QueryBuilder) is deprecated and will be removed from the standard library after 2027-08-17, to be maintained in a separate repository. Prefer the built-insqlORM syntax shown below for new code. Compiler deprecation warnings begin on 2027-02-18; until then the compiler emits a migration notice. See https://github.com/vlang/v/issues/27001 for details.
V has a built-in ORM (object-relational mapping) which supports SQLite, MySQL and Postgres, but soon it will support MS SQL and Oracle.
V's ORM provides a number of benefits:
- One syntax for all SQL dialects. (Migrating between databases becomes much easier.)
- Queries are constructed using V's syntax. (There's no need to learn another syntax.)
- Safety. (All queries are automatically sanitised to prevent SQL injection.)
- Compile time checks. (This prevents typos which can only be caught during runtime.)
- Readability and simplicity. (You don't need to manually parse the results of a query and then manually construct objects from the parsed results.)
For more examples and the docs, see vlib/orm.
Troubleshooting compilation problems with SQLite
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.
On Linux, you can also install the system development package instead:
- Debian/Ubuntu:
sudo apt install -y libsqlite3-dev - Fedora/RHEL:
sudo dnf -y install sqlite-devel - Arch:
sudo pacman -S sqlite
Interactive SQLite CLI
V includes a built-in SQLite CLI (v sqlite) as a V-native
replacement for sqlite3:
v sqlite mydb.db
It provides a full readline REPL with history and tab completion,
9 output modes, .dump, .import/.export, .backup, session
control, and schema tools. Run .help inside the REPL for the
full command list.
Convenience Methods
The db.sqlite module includes helper methods for common queries:
Using the sqlite VPM package
V also maintains the sqlite VPM package.
It wraps an SQLite amalgamation, but otherwise has the same API as db.sqlite.
Its benefit is that you do not need to install a separate system-level SQLite package or library on your system, which can be harder on Windows or musl-based systems. Its downside is that it can make compilation a bit slower, since it compiles SQLite from C in addition to your own code.
To install it, run:
v install sqlite
Then, in your code, use this:
instead of: