O Language

Database access

Connect to an O Language database backend, define a model and read or change records.

O Language pagesDatabase
On this page

The runtime’s database API has three parts: create a connection, describe a model, then use the model helpers. This interpreter revision supports postgres, mysql, sqlite3 and internal connection types.

SQLite example

def db = database("sqlite3", "profiles.db")
def profiles = model(db, "profiles", {
    "name": "TEXT",
    "desktop": "TEXT"
})

migrate(profiles)
create(profiles, {"name": "KDE", "desktop": "Plasma"})

def rows = fetch(profiles, {"name": "KDE"})
inspect(rows)

The model hash defines the columns used by the SQL schema. migrate creates that schema. create inserts values, while fetch returns records matching the supplied criteria.

Change and query data

The built-ins include update, delete, drop, truncate, query and search. Their behavior varies by backend:

  • SQL connections support schema migration and SQL queries.
  • The internal store uses a separate file-backed implementation and does not implement migrations or raw SQL.
  • search is implemented for the internal store; SQL backends use their query/model operations instead.
  • SQL query values can be passed as parameters. Prefer placeholders for data supplied from outside the program.

Read the built-in API reference for argument boundaries at the documented runtime revision. The interpreter and bytecode compiler maintain separate built-in registries, so check the execution paths guide before changing how a program runs.

In a web project

The web CLI can generate a SQLite connection and model file. It adds those files to lib/loader.olm. Review the generated driver, database path and columns before running the migration. See O Language Web Framework.

Database calls operate on the host process and configured database. Test destructive actions such as drop and truncate against data you can safely replace.

Next