While using same database file in web app and android app - android

I have a really different situation while using same database file in android app
and web app. Hope someone has idea about this problem.
I have a web application that has a database with full of data. After that, I am exporting
the file to my PC and PC to android device in .sqlite format. At this point, Let's say the last primary key id of one table in database is 25.
Now, android app will use that database file to operate. In the same table more data will
be inserted. Let's say 10 more. Now the last primary key id will be 35(25+10).
At the mean time, if I insert some more data into the database from web app, let's say 3
more. Then, the last primary key id will be 28(25+3) in the database of web app.
Now, I am pulling database file from android device and importing to the web app to keep
records
for future. The database file in the web has most of the data same as newly
imported database from android device because it was exported to android app before.
Now I want to merge the both database files to make one single database file and show record in web app. For that, what I want to do is;
Filter data from both databases not to keep same records.
Manage to replace the primary key id of android app's database followed by the last
primary key id of web app's database. Like 28+10 = 38 Now the newly modified database file has 38 records.
Please someone give me some technical idea because it is even hard for me to google. I am sorry if it is unclear. If it unclear, do not hesitate to ask.

Sounds complicated, maybe you should consider moving your data handling entirely to a cloud database: A relational database shared by an android app and a website - the easy way.
Alternatively I would suggest having two id keys on the android device, namely (_id, _id_server). This way, when importing the database from the web, the _id_server is populated and known and any future updates, from phone to web, will be easily filtered as they either have no _id_server value (new insert) or if the value is there (update existing entry).
I do not like the idea of altering id's on the web, can see a lot of problematic scenarios if doing so.

Related

Updating/Maintaining SQLite database after each App Release Xamarin Forms

This is my first time working on a Xamarin App and I am new to the app development world so I need some help figuring out this process.
Currently I run a php web service that generates some SQL files that I run in DB Browser and I get a database file which I then put into my Assets and Resources Folder. Using each platform's API I copy the database into a writable folder and use that to run my queries.
I followed this really helpful tutorial and it worked perfectly fine.
https://medium.com/#hameedkunkanoor/creating-a-sqlite-databse-and-storing-your-data-in-your-android-and-ios-application-in-xamarin-2ebaa79cdff0 .
After the "initial" setup I store a timestamp in a local table and and the next time the user opens the app I pass that timestamp and retrieve data that is older than that timestamp. The I update that timestamp and continue the process. That data is sent back in JSON format and make the updates to the tables.
My only concern is if a new version were to come out where I add a new table or a new column which is not present in the current version of my Database, how should I take care of those update Web Service calls? Is there a way of monitoring my DB version? I read somewhere where I could just ignore the new data that is not present already, like table or columns, but I'm not really sure how to do that.
I also saw that if I call CreateTable on my current tables I could potentially update them?
Also for future reference each time I develop a new app would I need to regenerate a new database file to store in the assets/resources folder? Is there a more automated process for this? Along with monitoring the version of my database?
Any Help/Tutorials/Suggestions would be greatly appreciated.
You have to remember that CreateTable it's already doing the columns update for you, because internally it calls a method called MigrateTable which you can see here for further clarification: https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs#L562.
However you could have to handle more advanced modification to your database, like adding triggers or something similar.
In that case i suggest you to perform modifications manually.
In Xamarin Forms i've ended up with this:
https://gist.github.com/matpag/b2545cc22c8e22449cd7eaf6b4910396
Could not be the best strategy ever but seems to work for me.
Summarizing :
You have to save the database version in an internal flag of the SQlite database called user_version accessible with PRAGMA keyword.
Every time you get the database connection, you have to perform a check and see if the current database version is the same as the app last database version.
If not you need to perform a database update and set the new current version.
Reference here.

How to alert users on new update to local database?

I'd like to receive some advice from all of you.
What is the best way for me to alert users on an update to my app? My app is a very knowledge-based & it works like a dictionary, so there will always be updates to it.
The database I have used is by DB Browser for SQLite, and they are all local database where it is uploaded into the assets folder in Android Studio.
Currently, the limitations are that:
1) it's obviously not real-time because it's stored locally;
2) every update I make to the database structure, I am required to upload the new database into the assets folder again, followed by uninstalling the old app on my phone, then run the app to install in my phone again so that the new database is overwritten.
I have read (How can I regularly update a database of content on an Android app?) & some others, and it seemed like I have to have a server, a cloud-based database & live app in market, to solve the limitations?
Is there really no way for me to overcome the limitations if I want to stick to a local database? At the same time, I kinda wish to avoid setting up a server because I am not intending to make the app live on market, and also this is just a school project I am working on and as such, I have very limited skill sets & knowledge about it and would like to make it on a school-project-based level.
Thanks in advance.
One way to do it is to connect to your local DB through local network instead of assets folder. Therefore, you can update the information by querying the local DB.
As for syncing the information between DB and your application, you should create a trigger or watcher that notify your application when the DB is updated. Therefore, your application can know when to query the DB for the updates. Another way is to just query the database periodically.
Bonus: you could move your database to a cloud-based database. Usually there are several providers that provide free database hosting up to a certain size, which should be enough for your project.

Copy data from website to realm database

My app needs to use data from different websites. On these websites, there is a search with different chemical ingredients.
How can I programmatically copy this data from website to my realm database?
This database on Realm needs to be saved as a table which has 2 columns: ingredient name, description.
First of all, create an ID for each ingredient, receiving ID, Ingredient, Description. It's the best way to avoid conflict.
Second, you need to assemble a crawler, and this goes beyond your android application, maybe a network service that can be done even in java, identifying the information on each site and inserting into your database. So doing turn your android application get this information from the site.

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.

Merging databases

At the moment I have apprx. 20 identical structured databases(SQLite) which I transferred from remote destinations (android phones) to my pc. Now I want to add them to a single one in order to perform data analysis on the data. How is this possible? The primary keys of the tables are auto-increment integer , so when using ATTACH I get a Primary key is not unique error. In addition it would be nice if i could somehow keep a reference between the rows and the tables which are coming from.
The way you worded the question, it sounds to me like it is a one-time operation you're doing on a home PC. I would probably just open a fresh database in the sqlite shell and attach/insert from/detach each database like they show on that link.
If you need to script this, this link has an example of embedding sqlite into shell scripts and this one shows some sql in a dos batch file. More preferably, you could code it up with your favorite language's sqlite bindings, like Python's.

Categories

Resources