SQLite Updating tables with read only data to App users - android

I am new to the android development and having certain concerns below
(i)In my app i am using SQLite database in which user should only be able to read the data (so i am running only reading the db query in app code)
db = openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);
db.execSQL(SELECT * FROM persons);
(ii)To display fresh data(eg news etc) to users i need to update my tables every day but i am not sure how to do that ?
PS: I know the code of how to make users to insert data by using an android app but here in this case only i want to insert the data(not by the users of app)
Can someone help me on this one?
Thanks! in advance

You can implement a SyncAdapter with periodic sync that runs each day to update the database.
https://developer.android.com/training/sync-adapters/creating-sync-adapter.html
Also you if you can integrate your backed with Google cloud messaging (or the newer FireBase cloud messaging) to notify the app to retrieve fresh data when available
https://developers.google.com/cloud-messaging/android/start
In both cases, you'll retrieve data from the server and update the db within a Service so that the updates take place even if the app is not running.
UPDATE:
In case of SyncAdapter, the data transfer code can be implemented in the onPerformSync() method. As for updating the databse, SyncAdapter plays nice with ContentProvider which makes the CRUD operations easier.
So in that method, you'll fetch the data from the network, open the database for writing and insert/update new data records. the SyncAdapter has a getContext() method that you can use for any context operations.
You can also check this application I implemented, it has a SyncAdapter and ContentProvider.
https://github.com/MinaSamy/WeatherApp/blob/master/app/src/main/java/com/bloodstone/weather/sync/WeatherSyncAdapter.java

Related

how to make a synchronous android application with sqlite

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.

How does Firebase Database Instances retrieve data in realtime?

I just built my first app. It's an Group Messaging App for which I used Firebase Realtime Database. I followed this tutorial to build my app.
The chat is working flawlessly and in realtime ie any changes into the Database are retrieved and reflected within seconds on my app. Actually, being bit curious, I didn't just copy paste all those code lines instead I'm trying to understand meaning behind each statement. So, I'm confused with one of my doubts:
How does this work in realtime (chats pop up immediately)? I was reading about Firebase Database here and they mention ValueEventListener is used to update app data in realtime but what's used here?
From the documentation:
Realtime: Instead of typical HTTP requests, the Firebase Realtime Database uses data synchronization—every time data changes, any connected device receives that update within milliseconds.
Network-wise this is achieved through WebSockets, which is used both on the server and in the client side Firebase library.
Additionally, the "Realtime Database API is designed to only allow operations that can be executed quickly".
Edit: The Firebase client library sets up one WebSocket to communicate with the Realtime Database, which is used for all the communication with the Realtime Database, both reading/subscribing and updating/pushing (unless you use the REST API).
Edit 2: In the tutorial you did you used the FirebaseListAdapter which abstracts away how the data synchronization is done. It's fourth parameter is a reference to a Realtime Firebase Database location that it will sync with (using WebSocktes), and populate the list for you. It takes each entry of the synced data and puts it into new Java objects of the model class you provide as second argument, namely ChatMessage.class.

Use Firebase DB with local DB

In my app I have SQLite db. I want to introduce sync between devices of my users. Firebase DB looks like an acceptable solution, but Firebase DB is cloud db at first. So, I can't use it as local db if user will reject auth dialog and let him use app, but without cloud-sync.
Now I think about combining my local SQLite db with cloud Firebase db.
For example, when user adds new row to local SQLite db, my app will also put data into Firebase DB. Other devices of this user will catch this event and update their local db. When the user uses authentification and installs app on new device, I want it to download all rows and put them into local SQLite db. That's my idea: use Firebase DB only for synchronizing data, not for storing it at device. Main reason for it is to let user use my app without authentification&synchonization. The second is that Firebase DB is not designed to be used as local db.
I'm right? Is it okay to use Firebase DB with another local DB?
Related question:
link He want the same as I want:
my plan is to offer the user the option to stay offline
If your firebase structure is not too complex you could also make a interface which defines methods like
void addData(Data data);
Data getData(long id);
void editData(Data data, long id);
void deleteData(long id);
then create 2 classes implementing that interface, one using Firebase the other using SQLite.
DatabaseImplementation
FirebaseImplementation
Inside your Firebase implementation, you would publish the data like normal, and publish one new node to something like root/requestUpdate/userId/push/ and push would contain information on where you request an update, and what deviceId published it.
Then have a ValueEventListener tied to that mentioned node, and if it gets a new child, have it look whether the deviceId is the same or not. If it is not, have the FirebaseImplementation getData using the information you got, and then use the DatabaseImplementation, to addData.
That would make sure that whenever a change is made, any other logged in client will know to update its firebase. If the client is not online, the next time he will be online he will do it as ValueEventListener triggers when it is attached. Make sure to loop through all the requested updates to make sure all are made. Also store the push keys of any updates you did complete on a local database that way you dont end up updating more than once.
Basically the firebase will always be up to date and store any changes a user made on a seperate node which is listened to by all clients.
Obviously this solution still has many problems you would need to fix, like figuring out when to delete the requestUpdate node. Logically after every user has synced but how do you determine this? ...
As for the first login, you would need to write a populateDatabaseFromFirebase() method which will do a whole lot of getDatas and addDatas. How you would do that will depend on how your DB looks. You then would store that the user has already logged in with SharedPreferences and the firebase UID.
That all being said, this will only work if your firebase is pretty flat. If you have a complex database, then everything becomes much more complicated and entangled and then it might be worth looking into an external library.
Some options for HTML5 hybrid apps
This is not what the OP asked about, but hopefully useful to some seekers.
You can use any combination of client and server database to implement storing remotely-maintained data in the device so it will be available when offline.
Some client options :
SQLite
(which is using the "native" browser database, works on iOS Safari
and Android webkit browsers)
IndexdDB
(another "native" option, but not supported in early Android, or
fully supported for iOS - so NOT a good option)
JayData
(which provides an abstraction layer from the underlying native implementation)
Lawnchair
(another popular client abstraction - I found the documentation lacking and have not used this for that reason)
Some server options :
MongoDB
RethinkDB
MySQL (for an SQL DB on the server)
and, of course there are many many more.

How to observe the changes made in Remote sql database table from android

Currently am developing an android application in which i wish to intregrate the webservices and save the received datas in my sqlite database.Further if there is any changes in my remmote sql i wish to update the same thing in my sqlite too.How can i acheive this..is there any way to acheive this.I just stuck in this thing for more than 2 days.
You have many options to perform the task:
You can ping the server for updates, for example if you upon starting call some API method getItems and store the items on local database, you can after certain interval call getItems again and replace the old items with the new ones
You can use a service like GCM (Google Cloud Messaging) to archieve this so your server sends the updated items straight to your phone, but this really depends on the application you're using it on

How to update an SQLlite database periodically through the internet ?

I am making an android application that needs to update periodically an SQLlite database of strings. Can updation be done through the internet, if yes how ?
I have a few lists in my application which are filled by the SQLlite database. The contents of the lists needs to change every day. That's why I need to update it everyday.
Yes you can update your Sql Database from the internet. See Guide to Services.
You can fire an Intent Service which does the work on a separate thread and there you can make a connection (for example HTTP GET request) and then put the parsed data in your appropriate database.
If you are using Loaders to populate your list, the list will be updated when you store new data in your database.
You may take following approach:
maintain a date variable in the shared preference or sqlite and on application launch check that if todays date is greater than date stored then you can call the rest/soap based web-services and fetch the updated record from server and update your sqlite table.In turn your list will populate the latest data...use cursorAdapter for that
OR
you can implement a GCM(google cloud messaging) service on your server,which push the updated data from sever to client every day.
OR
every time you launc an application you can hit the web service and upadte your sqlite database.
I hope solution caters to your problem

Categories

Resources