Learn how to store data in Anvil’s hosted database system
Anvil provides a robust Python-based database system built on top of PostgreSQL called Data Tables.
Follow this quickstart to create a database, get your app to store data in it, and read the data back.
Log in to Anvil and click ‘New Blank App’. Choose the Material Design theme.
In the App Browser, click the + next to Services.
You’ll see a list of available services and integrations. Click on Data Tables.
Click ‘Add A Table’ and name it
Click New Column and select ‘Add Number column’. Name the new column
Then add another Number column named
In the blue bar above the columns, there are some controls relating to this table.
Use the dropdown menu next to ‘Forms’ to select ‘Can search, edit and delete’. This gives client-side code permission to read from and write to this table.
Under Forms in the App Browser, select Form1.
Click on the ‘Code’ tab to see the Python code for
You will see a few lines of pre-written code. Your Form is represented as a class called Form1. It currently has
only one method, the
At the top of the file, import the
And at the end of the
__init__ method, write these lines:
base_number = random.randint(1, 100) app_tables.square_numbers.add_row(base_number=base_number, squared=base_number**2)
Now click the ‘Run’ button at the top of the screen.
Stop the app and look back at the Data Tables service. Your table now contains some data.
Run your app a few times to get some more data in the database.
Go back to the Code View for Form1 and add these lines to the end of the
for row in app_tables.square_numbers.search(): print("%s squared is %s" % (row['base_number'], row['squared']))
Run your app again and you’ll get the contents of your table printed to the Output Panel.
Click on the button below to clone a finished version of this app into your account.
Want more depth on this subject?
Read more about Data Tables.
Want another quickstart?
Every quickstart is on the Quickstarts page.