Get started

A real Python prompt.
Your database, ready.

The Asqueel CLI opens a standard Python REPL with `db` already available. This small example uses SQLite, so you can follow the homepage session without running a PostgreSQL server.

1. Prepare the example

You need Python 3.11 or later and SQLite 3.35 or later. This example is verified with Asqueel 0.6.0 and Asqueel Migration 0.3.1, installed from PyPI.

python -m pip install "asqueel[migration]==0.6.0"

Download the console example ZIP and extract it. Run the following commands from the directory containing console-demo.

asqueel check ./console-demo

The configuration declares one table, invc.region, with code as its primary key and name as a text column. The offline check validates the configuration; it does not create a database.

2. Create the schema

Review the SQL plan, then apply it.

asqueel db plan ./console-demo
asqueel db apply ./console-demo

The example creates console.db and the attached schema file invc.db inside the example directory. The configuration also accepts an absolute path through ASQUEEL_SQLITE_FILE.

3. Open the REPL

asqueel shell ./console-demo

The console tells you that db is ready. You can also register the configuration once and reopen it by name:

asqueel register console-demo ./console-demo
asqueel shell console-demo

python -m asqueel runs the same CLI.

4. Insert a record

At the Python prompt, get the table and insert a plain dictionary:

region = db.table('invc.region')
region.insert({'code': 'NO', 'name': 'North'})
db.commit()

The keys of the dictionary are column names, without $. The commit saves the change. Use a different code when repeating the insert: code is a primary key.

5. Build, inspect and run a query

query = region.query(
    columns='$code, $name',
    where='$code = :code',
    code='NO',
)
print(query.sqltext)
rows = query.fetch()
print([dict(row) for row in rows])

The result is:

[{'code': 'NO', 'name': 'North'}]

$code refers to a model column. :code is a bound value. Compiling with sqltext does not open a connection; fetch() executes the query.

6. Finish the session

db.rollback()  # Finish the read transaction.

Press Ctrl-D to leave the console. Pending changes are rolled back and the connection is released; exiting never performs an implicit commit.

You can remove the two local database files later if you no longer need the example.