O Language/Web/Database

From OnixOS
Revision as of 07:20, 25 August 2026 by Tedaryum (talk | contribs) (Created page with "= Database = Database programming has three common steps: open a connection, define a model, and apply schema changes. == Connection and model == <pre> def db = database("sqlite3", "example.db") def users = model(db, "users", { "id": "int", "name": "text", "email": "text" }) </pre> == Schema and data == <pre> migrate(users) create(users, {"name": "Ada", "email": "[email protected]"}) def result = fetch(users, {"name": "Ada"}) update(users, {"name": "Ada"}, {"em...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Database

Database programming has three common steps: open a connection, define a model, and apply schema changes.

Connection and model

def db = database("sqlite3", "example.db")
def users = model(db, "users", {
  "id": "int",
  "name": "text",
  "email": "text"
})

Schema and data

migrate(users)
create(users, {"name": "Ada", "email": "[email protected]"})
def result = fetch(users, {"name": "Ada"})
update(users, {"name": "Ada"}, {"email": "[email protected]"})
delete(users, {"name": "Ada"})

Use drop to remove a table and truncate to remove its rows. Use query for parameterized SQL and prefer parameters for external values.

def result = query(users, "SELECT * FROM users LIMIT ?", 10)

In a web project, olang web create model creates connections/main.olm when needed and adds the model and connection to lib/loader.olm.