I have a B4A app where the user can display a few dozen records from a table, modify any of the records at random, tweak some values to see the new results. He could do this for 10 minutes or so. He would not be accessing all of the rows from the table, maybe 10 to 100 rows at a time. Each row has around 1k to 5k of text in it so that would amount to 10k to 500k of data being loaded from the table in total for that session.
I am currently using DbUtils and it works just fine. Whenever the user updates a record, it gets written back to the Sqlite database.
But I got to thinking that it would be nice to add a feature where the user can roll back the changes he's made if he doesn't like the results after all that tweaking. So if he has changed 20 records and doesn't want to save it, he should be able to press an "Undo" button and all the changes he's made to the table will be reverted back to when he started (first loaded the data).
There are a few ways to achieve this.
Can anyone make a recommendation or offer a better solution?
TIA
I am currently using DbUtils and it works just fine. Whenever the user updates a record, it gets written back to the Sqlite database.
But I got to thinking that it would be nice to add a feature where the user can roll back the changes he's made if he doesn't like the results after all that tweaking. So if he has changed 20 records and doesn't want to save it, he should be able to press an "Undo" button and all the changes he's made to the table will be reverted back to when he started (first loaded the data).
There are a few ways to achieve this.
- When the rows are loaded from the Sqlite table (Table1), start a transaction. He can make as many changes as he likes and if he wants to save them, commit the transaction. If he doesn't want to save the changes, rollback the changes. But will this create a problem with the transactions that are being used by DBUtils because it means the transactions will have to be nested? Can I nest Sql1.BeginTransaction/Sql.EndTransaction?
- Can B4A use Sqlite SavePoints? A Sqlite SavePoint works similar to a transaction but with it is given name so the changes can be rolled back to the point when the "savepoint <name>" was started. These savepoints can be nested. See https://sqlite.org/lang_savepoint.html
- Instead of accessing the data directly from Table1, create a temporary table "TableTmp" and load it with the 10 to 100 rows from Table1 and allow the user to make changes to TableTmp. If he wants to save it, write the modified rows in TableTmp back to Table1. This will be slower because it will take more time to copy the data to a temporary table and then save the results back to the table. It will of course require more storage for the temporary table.
- Load the data from Table1 into a map and have the B4A application access the map instead of a SQLite table. This uses considerably more memory and I will have to handle writing the data to disk when the app is suspended. And it will have to be securely encrypted like it is with Sqlite.
Can anyone make a recommendation or offer a better solution?
TIA