Skip to main content

Using SQL

You can use SQL directly with the run_sql function. This is useful for complex queries or when you need to use SQL features that are not supported by the abstra API.

Selecting

    from abstra.tables import run_sql

all_users = run_sql('SELECT * FROM "users"')
print(all_users)
# [{"id": 1, "email": "michael.scott@dundermiflin.com", "name": "Michael Scott"}, {"id": 2, "email": "dwight.schrute@dundermiflin.com", "name": "Dwight Schrute"}]

user_id = read("What is your ID?")
data = run_sql('SELECT * FROM "users" WHERE id = $1', [user_id])
print(data)
# [{"id": 1, "email": "michael.scott@dundermiflin.com", "name": "Michael Scott"}]

Inserting

    from abstra.tables import run_sql

name = "John Doe"
email = "john.doe@example.com"
phone = "+1234567890"

run_sql('INSERT INTO "users" (name, email, phone) VALUES ($1, $2, $3)', params=[name, email, phone])