Store user data in database separately - android

What is the best practice to store user data separately from the actual app data? The user data is a statistic and it will be collected during app usage. The database must be always updated but I have to keep the user statistic untouched. Can I store for example the statistic on one table? but can I keep this table when the App will be updated?
Update:
Sorry, I think my question was misunderstood. What is the best practice to manage two kinds of Data?
Save all data in one database and save the User-Data in seperetly tables? or
Create two Databases, one for App-data and one for User-data?

I'm not sure exactly of your question, but yes, you can have multiple tables in SQLite. So you can have one table for the user, call it tblUserStatistics and then other tables for the app, or depending on the data, the app information could be stored in preferences.

Yes, you can store your statistics in one table, but it's structure depends on what you want to save. If you want to save only numbers, you can create a table with 2 columns (1st one an ID and the second one the value you want to save), and update your rows when your data changes. If you've got multiple types of data to be saved (numbers, text, dates, whatever), you must create different columns with different data types, but still, you can do it. For your other questions the answer is yes, your table will be kept after you update your app, because it gets saved in a database which doesn't get modified when the user updates the app, just make sure that when you create the new version you don't change the name of the database.

Related

How can I store database locally in flutter for read only access?

I have constant data that would be delivered with the app source and won't be changing in the future. Data size is few tables each +/- 1000 row x 20 columns. How can I store them in the app?
I should have the option to the filter data on several layers, like filtering table rows by certain value of column, and then further filter the rows by certain values of other column.
How can I load certain table asynchronously when the user opens the screen that will use that table.
Please provide sample code.
Thanks.
The cleanest way is to use a DB for local data. If you need them in a rows and columns way then an SQL like databse is your best option. I've use hive before which is a key value data store that is very good and efficient.
I suggest you add the data to the db on the application's first load and then just fetch when ever the user accesses it again.
Here's a good article that shows the difference between the most popular data stores for flutter.
https://blog.codemagic.io/choosing-the-right-database-for-your-flutter-app/

How to store two tables in Room Persistence Library of same data Type?

For example, Here is the data type,
User {
String name;
String lastName;
}
Now I'm storing this User class with Room and retrieving the list of all users with Dao
LiveData<List<User>> getAllUsers();
Now I want to add the Delete User functionality to it. And I also wanted to store all the deleted users in a separate table so that I can show them in a different place/Activity.
But I couldn't able to figure out what could be the better approach for this. Since the data type of deleted user is the same as User.
One way is to add an additional field into the User class and filter the list every time before displaying
User {
String name;
String lastName;
boolean deleted = false;
}
I just have to change the boolean value deleted when I delete the User object.
But I don't want to filter the list every time instead I want to store this deleted user in a separate table and this way I can retrieve the list of User and list of Deleted User much faster and save the time while filtering.
So how can I do this or is this the right way of achieving this delete functionality?
I guess there are two questions:
How to store two tables in Room Persistence Library of same data Type?
This question from your post' title
and
What is better - to store two tables or just one but to filter it?
I think this is the core of your question.
So,
1. How to store two tables in Room Persistence Library of same data Type?
The answer is simple. You should make two separate Room's entities, two separate DAO's for them (though you can use just one) and after deleting user you should in transaction delete it from one table and insert into another.
2. What is better - to store two tables or just one but to filter it?.
Of course, the answer is opinionated. You should consider all PROs and CONTRAs and make your choice.
Why to filter is better?
User and DeletedUser essentially are the same, they just has different status.
You don't need to duplicate the same fields in two tables. Otherwise you (and another developers who will maintain your app) should hold in mind that each change in User table (adding some field etc.) should be followed by the same changes in DeletedUser.
As usual Room's entities have id-s and they could be used in another entities as foreign key. As such you can't use User's id as foreign key, since after deleting it from User you can lose data in entities that are attached to it. In general, if User table is planned to have connections with another tables in app, then using separate DeletedUser is a bad way.
Why two tables is better?
As you've mentioned the filtered query might be slower. How much slower? That depends. For small tables (with up to 1,000 rows or even 10,000 rows) I guess you may not to see the difference. Still if you have some edge-case (enormous table) and struggle for performance really makes sense, you can choose that way.
P.S. Using Livedata in Room filtered result will be updated for you. So if you decide to use single table and one of the users in the table turns into delete-state, result will be updated without any special refresh-calls.

One mysql database per user with thousands of users

I have an Android app and i want to allow my users to sync their local sqlite db with server.Since i have thousands of user's how should i set up my MySql database on server?As i see it there are two approaches,
1) One database per user plus one database to store user's credentials.Is this even possible since there will be thousands of different databases on server?
2) One database that holds all user's data.I was thinking i could add a field (user_id) on each table that identifies a user.I don't like though the idea that all user's data will be on the same table!!!
What's the best approach for my case?Is there something different i could try?
One database that holds all user's data.I was thinking i could add a field (user_id) on each table that identifies a user.I don't like though the idea that all user's data will be on the same table!!!

How can I handle updating the database in my application and at the same time retaining user scores and settings?

I have a phone application that uses a database of words and tests a user to see which words they know. I have a SQLite database with the words that I populate using a console application and this is then deployed as a resource to phones etc.
When the user runs the application then it stores pass fail data in the same database but in different tables.
When I update the application a fresh copy of the words database is installed on the phone and all the user data is lost.
How is this typically handled? Do phone applications that use SQLite have multiple databases with one being used to store user data and the other holding data which can be brought in when the application is first installed or updated?
If multiple databases are used then is it possible to create a look up from one database to the other?
Thanks in advance for any help, advice or links that point me in the right direction.
I would use a file (JSON, or plain text) to ship the words with the app. Then, when the app runs, it reads that file and adds the new words to the database. This won't affect the other tables.
Instead of having to deal with that, we hard code the values into a static method in code. Then at runtime, we see if there is any data in the table and, if not, we grab the hard coded data and do an insert.
In your case, I would also just add a version number of some kind so then, if the version was lower or the table was empty, you do a delete all and then insert your new static data.

Simple MySQL database questions - Database per user of application?

Currently I have developed an android application that uses a local sqlite database per installation. The database comes pre-populated with static tables, and the entire point of the application is to allow the user to assign dates/comments with the pre-populated information in each table.
I am looking to bring this online, and move the database to a mysql format, allowing access via desktops and other mobile devices. Is the best way to handle this to assign each new user a new database?
I would strongly avoid creating multiple databases, and instead add relationships to the existing database structure you have with a users table. Each user has an association to each existing object. Keep in mind sharing with other users in the event that you may want to allow one user to see another user's info.
My suggestion is provide an update to the app where after the first launch after updating it pushes their information to your MySQL database and inform the users that they can access their data via other methods now.
how many user to you expect? I would use only one database with a user table instead of hundreds/thousands of databases.
One table for all users (only with user info like id, email, password, etc).
Another table with comments (with user id and his comment), so that you can add as many comments per user as needed. If dates are related to comments put them on this table, else another table for dates as well.

Categories

Resources