how to make a synchronous android application with sqlite - android

Unfortunately, I didn't find a better title for the question
i want to make application like a simple hotel reservation and i will make the database online if i add client to the hotel database via android application i want it pending and When I'm online i want the command execute but in the same time when i'm offline i want to see the that the command pending , it's like whatsapp when you send message while you are offline the message not send and you see it pending and when the internet available The message is sent automatically
so how i can do that ?

SQLite may well not be ideal solution. The reason being that SQLite is an embedded database not really designed for the client server model (the hotel database would, according to your decsription, be on a server). Appropriate Uses For SQLite
Perhaps Firebase Realtime Database
could be more appropriate.
However, if you wanted to use SQLite to just handle the requests from the device SQLite could perhaps be used for that aspect. For the requests to be sent automatically you'd need to look at perhaps running a service. Services overview. You would probably want to incorporate Notifications
If using SQLite then the first step, database wise would be to design the database, the table(s) and the columns within the tables. However, to do so requires that the system itself is designed. System design, would depend upon what can actually be done.

Related

How to notify my android app when something change in MySQL Database

I have a question about real time notification in MySQL.
I want that when there is a change in a specific table of the MySQL db, the db notify my android app that do something.
Is it possibile to do? I have listen that trigger can help, but i didn't found anything about that in web, especially for android.
I'm creating a real-time chat app for an android project at the university. It is required to use a servlet in the project, so my servlet makes requests to the database and sends the data to the app. To do this the app makes a GET request to the servlet every second to get the new messages. I would like to avoid making these requests and updating the message list only when there is a change in the database. I know it can be done with firebase for example, but having to use the servlet I would like to know if there is a way to notify the app when this change occurs.
The messages are insert in the db with a POST request to the servlet that update db; information about messages are taken from an edit text in the app and with a send button i make the request, so the thing that i have to do is to push the android app when new messages are detected in the db.
Presumably you have some code component sending commands to the database. If yes, then that is where you could add code to post notifications to your app via websockets or something similar. Triggers in your database come at a cost and should be avoided if there is going to be a lot of them required.
As already mentioned, the app constantly polling, looking for a change is also expensive and probably best avoided.
'Is it possible?' questions are always difficult to answer. The answer is almost always 'yes', but 'is it advisable', 'is it a good idea', 'is it recommended', that is where it gets more complicated. I don't know of any way that a MySQL database change could notify individual Android app installations after a change. However, a running instance of an Android app could poll the MySQL database and look for the structure of the table, and if the structure of that table differed from the last time it polled, it could do something.
This would have a large overhead, in having the app continuously polling the MySQL database to check the structure of the table. That would add a lot of demands on your MySQL installation, eat up a lot of the app user's bandwidth and data plan.
So, 'is it possible', yes. 'is it recommended', no.
One question would be, what is the scenario where you think the table structure might change, and how often might that occur? What many people do, is check to see a database's table structures only once, when the app starts up a new session. If a change is detected on start-up, it could make the necessary local changes to its logic, then, assume the structure will remain constant through the rest of the session. This would eliminate the constant polling and other negative behaviors I mentioned before.

How to synchronize SQLite and MySQL databases using push notifications?

I have an SQLite database on Android and a MySQL database on a server. I want to synchronize these databases when a user edits data on their phone or edits data on a website.
I know how to update the MySQL database on the server when a user makes changes on their phone but I don't know how to update the Android database when a user makes changes on the website.
I have read into push notification and believe this to be a good path to follow but I have a few questions about it:
When a user updates data through a website it will send a push notification to that user's phone saying changes have been made. Can this push notification trigger to update the Android's database with the new changes made on the Server database?
What if a user turns off push notifications? Will I still be able to trigger for their Android database to be updated?
I have also read up on SQLite and MySQL database synchronization and found this post SQLite and MySQL sync but did not find the post helpful for my situation.
Are push notifications a good way to go or should I be using a different approach?
In a nutshell - I want a way for the Android device to detect changes on the MySQL database and update its SQLite database without the user initiating the synchronization.
I'm afraid I've not used push notifications. But a solution might be: You could create an early method call to an Asynchronous polling event from the launcher onCreate() that looks up the server to see if any changes have been registered (though an API of some sort) in the MySQL, and then update the SQLite that way? Since it's the first thing that happens on launch, technically the user isn't initiating it. Granted this won't update during use of the app, unless you repeat poll at regular intervals?
Token based pagination approach.
Assumptions: or calls you need to take
One of the databases will the source of truth, in case of differences in the two, which data is true, and which will be overwritten? - assuming remote database is source of truth
What's the frequency of data changes? - assuming its not realtime critical
How much stale data are we OK with dealing on the app. - assuming we're OK with a few minutes of difference
How to ensure data is consistent
Find a method to associate a token, which can be used to identify till which record is the data in sync. This is important no matter how assured you are of web requests, they will fail. So the best method is to send the last token that have stored, and the web endpoint will return data from that token to the latest value.
If the volume of data is too much here, sending chunks > sending all of it. Chunks work the same way, based on tokens.
These tokens can be simple PK auto increment column as well.
How to deal with time difference, stale data
If your application demands some data to be near realtime, better to categorize data based on a fiew screens, and whenever the user comes to the said screen, send a request in background to fetch related data columns only. Will ensure the data stays in sync w.r.t to the important columns. This is a classic push/pull approach. Can also be done on the splash screen itself.
as a rule of thumb, if you need something urgent, pull.
if it can wait, wait for a push.
Push notifications are OK as far as:
they should be silent.
there'a a limit on the number of push notifications that you can send
have costs associated
what's the fail - check mechanism? What if the requests fail?

How to send update SQLite table from one to everyother

I have 2 users in my app.
1) Normal user
2) Admin
The admin can perform certain changes in the app which update my SQL table stored. Once this update is done how do I make it affect the users who aren't admins?
I'm guessing this will have something to do with a server but how?
EDIT:
Its an app that holds data of what classes are going on in different classrooms in different buildings. So I'm storing all that data in a table. so now if the admin wants to change something I make that changes in the table accordingly. But this change will happen locally on his device. How do I make it appear for all other devices?
In that case, you have to make a web server and migrate the database to that server, and all the users can access the database from there. And to reflect updates to every user, u can implement 2 methods:
The users will poll the server at regular intervals to check if there is any change in the database
u can use GCM, i.e. Google Cloud Messaging to directly send notifications to all users as soon as the database is updated.
While the first method is easy to implement, it could eat up bandwidth of the user as regular checks consume more data.
The second method is lengthy to implement, but it saves up a lot of bandwidth.Also, GCM works only on android devices, so it kind of redeces the scalability of the pp to only the android platform.

Android notification when sql row is added

How would one go about creating a notification when a row is added to a remote SQL server?
For example, if user A inserts a row into the SQL table via a PHP script is there a way for user B to get some sort of response from the server to know to display a notification something has been added to the database?
Any help would be greatly appreciated
Depending on the underlying database (I've only done it in mySQL and Interbase/Firebird), there's usually a mechanism available to have an update/insert trigger either update another table (which you could pull) or broadcast a message on a socket (which could then generate a notification).
That said, a good web application is build around a framework, and those frameworks usually abstract the database interaction. In short, find the spot in the php script where the row is inserted, and hook your notification code in there. It'll be much more reliable, and more instantaneous.
As far as notifying the remote application, you might want to look into GCM.

approach for synching android app database with server db?

I'm developing an Android app as a "proof of concept" for our company. If they like it and think it's worth investing, then we'll move on to bigger things. I'm trying to figure out the best/most practical approach for this.....the basics of the app will connect to our DB and display information regarding a specific customer. For now, let's say we will only pull data from 3-4 tables (but there could be 10+ in the future). If the app doesn't have an internet connection then it should use the local DB. What is the best approach for this? Here's what I was thinking and would like some input/suggestions if possible:
1.) app runs checks internet connection. If exists, check db version (how, through a web service?)..if server db is newer, get latest data. If no internet, use local db.
2.) app parses data and displays it.
If this is correct, then there could be no modifications to the web service that would add fields to a result without changing the app as well. Is there a way for an app to parse fields regardless of how many fields there are?
I've read and walked through the tutorial on google with databases and such (Notepad tutorial) but it seems like the column names are all hard-coded in the parsing class, which I was hoping to avoid.
Sorry if this is confusing but I know I need my app to use a local db to read data, I also know that the app must get data from the server when it can (via onCreate or a refresh button) and copy it locally....Copying it locally is the part I'm having trouble understanding I guess....is there no way of saying "go out and get this result and display it", knowing that those results could mean 5 fields the first time or 1 the next.
Any help/guidance is greatly appreciated!
You probably want to use a SQLLite DB to store your data locally, a ContentProvider to provide CRUD access to the db, and a SyncAdapter to sync with your server when possible. The Sync Adapter also writes to the DB via the ContentProvider. See the SampleSyncAdapter sample in the SDK for an example of how this works. You will be implementing your own ContentProvider, but the sample just uses Android's supplied Contacts ContentProvider.
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

Categories

Resources