✨ Announcing our seed funding led by Lux Capital! Read more about it on our blog

Search documentation...




[ Learn ]

[ Concepts ]

[ Reference ]


Queries are used to retrieve data from a database.
A query is a request for information from a database table or combination of tables. A query can be used to retrieve data from a single table or multiple tables. A query can also be used to insert, update, or delete data from a table.


To execute a query you must first create a rx.session. You can use the session to query the database using SQLAlchemy syntax.
The with rx.session statement will automatically close the session when the code block is finished.
The following example shows how to create a session and query the database. First we create a table called User.
class User(rx.Model, table=True):
    username: str
    email: str
Then we create a session and query the User table.
class QueryUser(rx.State):
    name: str
    users: list[User]

    def get_users(self):
        with rx.session() as session:
            self.users = (
The get_users method will query the database for all users that contain the value of the state var name.
Similarly you can use the session.add() method to add a new record to the database.
class AddUser(rx.State):
    username: str
    email: str

    def add_user(self):
        with rx.session() as session:
                    username=self.username, email=self.email
← TablesDeploy →

Copyright © 2023 Pynecone, Inc.