How to retrieve datas from a tinydb


#1

i want to save datas from 4 textbox with a button save! and on screen 2 i want to retrieve a list of all data saved for each user!

is this correct to save?

how i can show values in screen2?


#2

If your Screen2 was also given a TinyDB component, then it can extract the information saved on Screen1.

You block will work properly in this respect. However, the way it is configured, you will keep adding more and more items into your “lista” variable, under the same tag. You may need logic to review the content and erase the parts that are no longer relevant. Or, better yet, give each save its own unique tag, incrementing the tag name with a suffix (db_1, db_2, and so on) so that each could be individually accessed, edited, and perhaps even erased if needed.


#3

try this
Screen1
1

Screen2
2


#4

@Django

I do not think it is optimum to return to Screen1 by opening it, as opposed to closing Screen2.
I am not sure how it will perform on Thunkable Classic, but way back when, on MIT App Inventor, swapping between pages by opening it meant opening another instance of the page in question, and accumulating a lot of background parameters, leading to an eventual slowdown and even crash of the app.


#5

Correct . You should always close a screen when you open a new one.


#6

True, I missed that. I just wanted to show him how to use a list and tinyDb to transport his data to the other screen based on his own template. So, add close current screen whenever you change screens.


#7

thanks!

but with this way i can see only the last list i enter?
datas are overwritten each time? and i’ll see always the last list that i enter?
can i store more users and then with a button search on the db retrieve data that i need?


#8

Each call to TinyDB.Store Value that uses an existing tag overwrites whatever was stored with that tag before. If you want to append, you either need to extract the current record value and add the new data to that record before saving it back, or you need to save the new data using a different tag.

The second approach (different tag for each ‘user’) makes a lot more sense since you deal with manageable blocks of data each time and let TinyDB deal with details of the memory management.


#9

now i’ve solved the first problem

but i cannot store the date of datapicker!

how i can solve this?


#10

The DatePicker.Text value is, by default “DatePicker”.
Even if you use the date picker to enter a date, that does not change the text unless you have a “DatePicker.After Date Set” event that captures the date, formats it, and feeds that into DatePicker.Text.

For the record, if you want to use a date as a tag, that more or less restrict you to one entry per day, as an afternoon entry would overwrite the morning.


#11

image

solved!


#12

Now I have another problem if I search for a tag that doesn’t exist I receive an error


#13

What did you expect?
From your blocks, if the tag is not there, the value for lista will be " " since this is what you set as “valueIfTagNotThere”.
That means you have to add some logic to issue a warning “Error: tag does not exist” to the screen if “lista” is not a list, and if it is, THEN you parse it and populate TextBox1, TextBox2 etc.

You either write the code to handle exception gracefully; or the system will issue them, bluntly.


#14

If you use tinydb and you want store unique records for an user too I storage data with following way: user,now(number),val1,val2,val3,val4. Now(number) is like ID because it is very difficult to repeat that number.

You can recover data from any user and date. Although with that structure I would use a database like mysql