Updating/editing/reading data table within code

I’m new to anvil and python as a whole. I’m trying to make it so when a button is clicked a specific point in the data table is edited or an element’s property is set to a specific item in a data table. I can’t figure out how to target and edit a specific cell in the table (i.e. target a value at this row and this column).

I’m sure there’s a simple solution, but I couldn’t find anything that solved my issue. Any help is appreciated!

Welcome to the forum!

If you haven’t already been through some of the tutorials, you should give them a try. Especially the News Aggregator tutorial: Anvil | Build Database-Backed Apps

That’ll walk you through the whole range of data table operations, including updating values in rows.

1 Like

Yes definitely go with the tutorials. They will teach you the basic ideas.

Essentially what you will learn to do from them is:

  1. Search your table for a specific row by looking for some data in that row that matches what you want to work on. This row will be turned into a variable that is a row object.

  2. Edit that row object by specifying what column (by its name) in that row you would like to change, and what you would like to change it to.

1 Like

First off, thanks for letting me know about that I had no idea the tutorials existed, however I can’t seem so solve my issue. It’s very possible I’m just a complete idiot, but here is the code returning the error:

self.likeButton.text = str(app_tables.uploads.get_by_id(len(app_tables.uploads.search())-1)['likeCount'])+" likes"

Essentially I am trying to update the text of the like button to the amount of likes in the last row of the data table. Based on what I understand from the tutorials, I’d think this is correct, but I guess not.

Similarly this code:

app_tables.uploads.get_by_id(len(app_tables.uploads.search())-1)['likeCount'] = app_tables.uploads.get_by_id(len(app_tables.uploads.search())-1)['likeCount']+1;

Which is meant to increase the likeCount column in the most recent row by 1.
Both of these return the error:

TypeError: 'NoneType' does not support indexing

Any ideas?

I found this by searching the forums for “last row of table”

The real burning question is why does it have to be only the last row that holds this ‘like count’ value?

What would happen if you had 2 users using the app at the same time?

Instead of telling the system ‘just give me the last blah blah’ it might be better to label the data in the database with a column, and probably add another that stamps the created and or last-modified time as well.

This way you can sort by time, or search by some sort of label, or both.

1 Like

Yes using the article you sent earlier I just found a better way of doing this by using the upload time. Thanks so much for your help and apologies for being ignorant about these things haha

You will find we are very helpful here, but our goal is to get you to figure it out for yourself. Bombarding you with a bunch of code we wrote to fix your problem won’t teach you how to use anvil beyond the next few minutes.

Good luck :vulcan_salute:

2 Likes