Run SQL
To edit data from inside your projects, you can run SQL from your code. Just import the run
function and write plain SQL.
from abstra.tables import run
from abstra.forms import display, read
all_users = run('SELECT * FROM "users"')
all_users # [{"id": 1, "email": "michael.scott@dundermiflin.com", "name": "Michael Scott"}, {"id": 2, "email": "dwight.schrute@dundermiflin.com", "name": "Dwight Schrute"}]
display_pandas(all_users, label="All Users")
id = read("What is your ID?")
data = run('SELECT * FROM "users" WHERE id = $1', [id])
data # [{"id": 1, "email": "michael.scott@dundermiflin.com", "name": "Michael Scott"}]
display(data)
from abstra.tables import run
from abstra.forms import read, read_email, read_phone
name = read("What is your name?")
email = read_email("What is your email?")
phone = read_phone("What is your phone number?")
run('INSERT INTO "users" (name, email, phone) VALUES ($1, $2, $3)', params=[name, email, phone])
Query DF
Using query_df
instead of run
returns a pandas DataFrame:
from abstra.tables import query_df
query_df("select * from users")