How can i update sqllite dynamically? - android

I have set app my sql lite database for my app. Is there any way to update the database without republishing the app? For example i want to add a new user in the sqlite database. Do i have to upgrade the database version on my app and publish it again? Is the remote database the only solution for dynamic android database?

If i've understood, you want "deploy" a new db for your app. You can do that but you need root access because in other case you don't have permission. Something like:
adb push yourNewDB data/data/this.is.your.package/databases
Then enter in shell:
adb shell
Then kill the process for your App:
ps
kill pid

There's no simple way to upgrade your app without republishing it unless you have an update system integrated on your app.
The simplest (and standard) solution is to republish and upgrade your DB version number, then the onUpgrade() callback will get called. There you should check the current DB and upgrade as required. Following your example, you just have to check if that user exists and if not create it.

You can remotely provide updates to your DB via an online service. This could provide whole new DB file for file replace or alternatively provide delta CRUD updates via xml or json. But you will need to code your update method into the app before publishing.

Related

How to protect sqflite database when updating flutter app

I use an sqflite database in my app. The database is filled with entries by the users. It is essential that the database is unchanged when I deliver a new app version.
When I deploy a new version of the app as apk to my real phone the database is deleted. Is there a way to protect the database during update?
I wonder if it is the method how I deploy the new app version? I use "flutter install". Maybe the uninstall method removes everything from the app including the database and creates a fresh app directory!?
flutter install deletes app and database. The solution is to use streamed install. Instead of flutter install, run this: adb install build/app/outputs/flutter-apk/app.apk. This will update the app and leave the database intact.
You must have sdk platform-tools installed and an environment variable pointing to the platform-tools.
You can get the contents of old database and store it in temporary database and save all the old content in new database and after successful insert delete temporary database.
In short following pseudo code may help:
tempdb.data = getOldDatabaseContent
newDatabase.data = tempdb.data
delete(tempdb)

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.

manage non local sqlite database in android

I have started working with databases lately, I was able to create a local database and manage it in my Android app. Now I want to move to higher level with it but I don't know how to do it.
The users in my app need to be able to modify the database, for example insert new data in it. When a user insert new data in the database I want other users to see this change in their copy of the database.
I understand that I will need to store that database on a server or something and synchronize it with the users.
Can anyone tell me the steps to do so?
You should perfom this task in steps.
First, make the local database, and use a system to know when/what changed.
I usually work with triggers myself, but any "mark" is enough to synchronize.
Then, you must make a replica of that database somewhere else. Realise that maintaining the databases is a process, any change in the structure of one database must be performed in all other as well.
Finally, you must implement a method to transfer the data.
So, for an example:
db_local the database in the device.
db_outside the database in the internet.
db_local.trigger -> onInsert
On the applications, check for internet, then connect to your server, then upload the same command to db_outside and run it...
In this step, you must handle connection issues, and if the SQL command was succesfully executed, you have replicated the database content.
Once you have the replicated database, inform a system (like google cloud messaging), that the database was changed, and have the other users pull the info.

Locking SQLite database file during backup

I am trying to implement a service to backup the SQLite database of my Android app. I am planning to both schedule this service for frequent backups (every day for example), and add an option to launch it immediately.
My problem is that the service might start while the application is running, or the user might start the application while the backup is in progress. And they may write to the database while I am copying it.
Is there any way to make sure that the copy and write will not run concurrently, without adding synchronization locks to all my queries ?
Thanks !
If you are not using explicit transactions, SQLite will automatically use a transaction around each SQL statement.
To ensure that the database files cannot be accessed by another database connection while you are doing the backup, open an exclusive transaction around the backup.
SQLite site has some notes on doing hot backup on a running database. See the Example 2 in that page.
In android, if you want to initiate a file copy of your sqlite db file, you will first need to get a shared lock as mentioned above, but this approach has shortcomings.
Ideally, you would want to use the sqlite3_backup_* apis.
These APIs are not available in standard android sqlite API, but it is easy to copy the sqlite jni code to your project, and expose these additional features. The advantage with this approach is that you dont have to change existing API calls in your code, as it mirrors existing android sqlite API definitions.
To expose backup APIs, take a look at android_database_SQLiteConnection.cpp to see how existing JNI functions call the native sqlite_* APIs.
Another option is to use something like sqlite4java, it has the sqlite backup APIs wrapped in as Java APIs, and seems the latest version supports Android.

Categories

Resources