SQLite - How join data from multi database and query - android

I have this scenario:
one database in the app and many database that are downloaded from different devices and saved in the same database folder:
mydatabase.db // database of app
device1_mydatabase.db // database from device 1
device2_mydatabase.db // database from device 2
device3_mydatabase.db // database from device 3
....
...
All databases have the same structure , because are the same app in different devices.
I have one query that obtain the total production from the mydatabase.db.
I would like to obtain a total query from all databases.
1) is better to create a new database copy and insert all data from the single database and then query the total or there are different better approach?

This may be a matter of opinion, but I think it is better to load the data into a single database. This gives more control for the application. Here are four reasons that come to mind:
You can optimize the database for the queries that you want to run (say by changing indexes or restructuring tables).
You can incrementally load the tables into the master database as they become available.
Adding another device does not require changing the final queries that are run (just the loading code).
If you change the underlying database structure, then you will have an issue where the databases are not identical. Having a layer in-between isolates the final queries from the original databases.

Related

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.

App local database

Not sure, if this is important: app is written with react-native.
Question is about both iOS and android apps.
I have a static database, which contains currently ~9000 rows, each row contains 45 columns and about 280 letters total in it. So, basically, database is relatively small. I'll need to perform pattern search (equivalent of ILIKE in Postgres) and sorting based on misc columns with numeric values. No insertions, no modifications, no relations with other "Tables" required.
Should i write nodejs server, instantiate PG database, connect app through web-socket and start querying data from pg, or should i just somehow create local database in app and search through it right in app? The local db is way simpler but I'm worry about performance. If nothing wrong with performance, then what if the database will grow to 15000? 50000?
If the database can be local and there's no need for it to ever be persisted on servers, then you should probably explore Realm. I have not heard of how it great it performs with a lot of data but it acts as a database engine in your phone and has indexing as well.

SQLite Implementation Android

While Implementing SQLite data base in android
In my Application I have different 5 database tables.
I found two ways to implement this
1)
create different database for each table and respective SQLiteOpenHelper implementation.
2)
Create 1 database and only 1 SQLiteOpenHelper implementation in that, create all the required tables
I have below queries regarding the above methods.
a) The SQLiteDatabase db = this.getReadableDatabase(); will get database in RAM to operate on It
In case 1) we have a separate database for each table so it will load respective database in memory ?
In case 2) We have single database having all the tables so the all the tables will come in RAM ?
b)In case 2 What if one feature updating in one table and other in 2nd table and 1st feature completed its task and calls close on database then what will happen to the 2nd feature which is still in process ? will there be any exception ?
This is mainly a design question.
If your tables make a consistent whole altogether, they should be stored in a same database. If they are independant, completely unrelated, with no common purpose or use case, these are as many databases.
For example: the tables Users, Accounts, AccessRights should be in a same database as the Users do have Accounts that are granted AccessRights.
Keeping data consistent across multiple tables (the model) is actually the purpose of a database.

Synchronise/update sqlite databases

We've got an android app and an iPhone app (same functionality) that use sqlite for local data storage. The apps initially come with no data, then on the first run they receive data from a remote server and store it in a sqlite database. The sqlite database is created by the server and the apps download it as one file, which is then used buy the apps. The database file is not very large by today's standards, but not a tiny one either - about 5-6 MB.
Now, once in a while, the apps need to refresh the data from the server. There a few approaches I can think of:
Download a new full database from the server and replace the existing one. This one sounds like the simplest way to deal with the problem were it not for a repeated 5-6 MB downloads. The apps do prompt the user whether they want to download the updates, so this may not be too much of a problem.
Download a delta database from the server, containing only the new/modified records and in some form information about what records to delete. This would lead to a much smaller download size, but the work on the client side is more complicated. I would need to read one database and, based on what is read, update another one. To the best of my knowledge, there's not way with sqlite to do something like insert into db1.table1 (select * from db2.table1) where db1 and db2 are two sqlite databases containing table1 of the same structure. (The full sqlite database contains about 10 tables with the largest one probably containing about 500 records or so.)
Download delta of the data in some other format (json, xml, etc.) and use this info to update the database in the app. Same as before: not to much problem on the server side, smaller download size than the full database, but quite a painful process to do the updates.
Which of the three approaches you recommend? Or maybe there's yet another way that I missed?
Many thanks in advance.
After much considerations and tries-and-errors, I went for a combination of options (2) and (3).
If no data is present at all, then the app downloads a full database file from the server.
If data is present and an update is required, the app downloads some database from the server. And checks the content of a particular value in a particular table. That value will state whether the new database is to replace the original or whether it contains deletions/updates/inserts
This turns out to be the fastest way (performance-wise) and leaves all the heavy lifting (determining whether to put everything into one database or just an update) to the server. Further, with this approach, if I need to modify the algorithm to, say, always download the full database, it would only be a change on the server without the need to re-compile and re-distribute the app.
Is there a way you can have a JSON field for each of the tables? For instance, if you got a table named users, have a column named "json" that stores the JSON for each of the users. In essence, it would contain the information the rest of the fields have.
So when you download the delta in JSON, all you got to do is insert the JSON's into the tables.
Of course with this method, you will need to do additional work in parsing the JSON and creating the model/object from it, but it's just an extra 3-4 small steps.
I will recommend approach 3, because app will download the json file more fast and local db will be updated more easily avoid overhead of more internet usages.
Just create a empty db initially according to server db and then regularly updated the same by fetching json

Android: Dealing with SQLite DBs in my program

I am making a pokemon like game that will utilize SQLite DBs for storage and I'm trying to figure out how to best set it up.
I would like to have three tables:
One for all the "pokemon" and their stats
One for all the "pokemon" moves
One for the player's "pokemon"
I was thinking of having the app download the DB from my website so that I can add new "pokemon" and update/balance stats, but I want the player's "pokemon" list to stay the same unless they add/remove them from their teams.
Should I set up a DB with 2 tables for the "pokemon" and moves, and a second DB just for the player's "pokemon"? And how should I manage downloading/storing the DB? Currently I have it set up to download the sqlite DB to the SD card then have the app create a new SQLite DB passing the file location. What could I do to update the DB after I've made changes?
My advice is to change the way the database is being updated.
Instead of downloading the whole database as a file, as that seems to be what you're doing, a more suitable solution could be to have the application look for updates through a webservice and, if new updates are available, return them as XML / JSON / etc.
Your application could then parse the received data to generate SQL statements that would update the database without affecting current player stats.

Categories

Resources