Transfer data from a mysql database to a sqlite database.(Android-JSON) - android

I am developing an android application that has a sqlite database. I want to transfer data from a mySql database that hosted in a web server to the sqlite database. This data transferring will happen when a user install the application and when there is a change in the mysql database. To achieve this I hoped to uses a JSON web service. I know two ways to achieve this task using a JSON web service.
Use separate JSON responses to transfer data from each table of
mysql database.
Use a single JSON responses to transfer data from all the tables of
mysql database, in once.
My question is: what is the best way to achieve my task when consider the speed and reliability?
(1 or 2)

Well, this question is primarily opinion based. anyway in my opinion you can create an extra table for recording changes from all the other tables in the database
ie. If you have 3 tables like
1.table 1
2.table 2
and
table 3
Add another table like changes_table or something with fields like table_name and isChanged (boolean). and listen for the isChanged field and sync only that table because it is the best way to ensure data consistency and yields more performance (since transferring the whole database is obviously a heavy and slow process over network).

It depends on the database size and your frequency for syncing,
if the database is large in size or will be in future, then the best way to go is fetch data for each table whenever you need it. In case if the data in a single table is too large then you should implement it by paging i.e. pass the parameter in URL for page.
otherwise if the data is not huge then the second way is great that you create a web service that will iterate over all tables and return the JSON for all records.

Related

insert large data from file into android sqlite database

I have around 9000 json records with 7-8 attributes stored in file. I have to insert that data into sqlite database. What is the most efficient way to do so.
So far, I am converting that file data into jsonarray and convert that into arraylist and then insert that list into database, which definitely doesn't seem to be an effective way
Edit :- this file already exists in resources folder, so the data needs to be inserted in the database at the beginning
Do not ship JSON with the app, but instead ship a SQLite database with the app.
For cases where the JSON is coming from some other place (e.g., a Web service), make sure that you use transactions when updating SQLite with that data. By default, each SQL operation is a single transaction, and transactions are a bit expensive. It is far faster to insert 9000 items in one transaction (or 9 transactions, or 90 transactions) than it is to insert 9000 items in 9000 transactions. If you are using SQLiteDatabase, use beginTransaction(), markTransactionSuccessful(), and endTransaction(). If you are using Room, use #Transaction. If you are using some other database API, consult its documentation for how to set up your own transactions.

Android update multiple local SQLIte tables, from remote MySQL server(PHP), what should I do?

I need this for a custom app, built for a specific company, so it will not be on Google Play.
I have an app, that uses about 15 SQLite tables. It needs to be able to work offline and online, so I have to use SQLite to keep information for the offline part.
The main idea is that when user on Android touches a button, an update process is starting.
The logic of the update is:
For each local table I have a AsyncTask class dealing with the update process described bellow
Every local SQLite table have an "_id" field (autoincrement) AND "idremote" field to be filled with MySQL id of the record (plus the rest of the fields)
Each time the update starts, the app prepares a string containing all "idremote" id's from the local table and sends it to the server
On the server side, a PHP file receives the string of ID's and checks in the MySQL table, to see if there are new ID's that are not in the received String of ids (each AsyncTask has it's own php file on the server)
If the PHP finds in MySQL table new ids, then it sends the new records using JSON back to Android
I process the resulting JSON in onPostExecute of the AsyncTask and insert the new records in the SQLite table
So I have 15 AsyncTask classes that all perform the same operations as above, each of them dealing with a specific table.
I also do the update of the remote tables sending the new records to MySQL through the same mechanism
My problem is that I want to be able to see/know when the update is done/finished so I can notify the user of that fact, but since we are talking about multiple AsyncTasks ... that run simultaneously... I have no idea how to implement this. How to find out when all the AsyncTasks are done?
Or is there a better way to do this task? The update of the local/remote tables?
Thank you
The best way to do this would be using a SyncAdapter which is designed to handle syncing changes to a remote server in the background.
You can find more information here: http://developer.android.com/training/sync-adapters/index.html
You should ignore the section "Creating a Stub Content Provider" and create a content provider to access your existing SQLite database.

Get DB-Data from Server

I have a problem with my application. I need the data from my MySQL-Database on the server. Usally I'm using HTTP-Posts, but this time I'm have to get a lot of db-Entrys. So I thought, that i'm just copying the database to the device. But here is the next problem: The Database on the device has the same structure like the db on the server, and additional 2 extra tables, to save some local data.
Finally my question is, how to get a lot of data from my database?
One extra question: is it possible/effective to use 2 local databases? So i could use 1 for local data and the other one for the server data. Then it would be possible to copy the db, but i need also an mysql-query, because i don't want to copy the whole data.
Yes, is possible to have 2 local db, and the best way to download an entire db from server is to dump the mysql db on server to a sqlite db, and so you may download the sqlite db from your mobile app.
Well choosing a database is depends on your project requirement. If your data is getting updated frequently on server and you want the display the updated data to the user. Do not copy database in mobile. Instead of it use webservices to get data from remote server.
And Yea, you can create two database in Android, nothing wrong in it. But again I would suggest that for only two tables do not create separate database.That can be merged in single database.
Summary
Frequently Updates in Data : Use MySql + Webservice
Static Data : Go for local db

Sending SQLite db to web service

I have an android app which populates a SQLite database with numerous latitudes and longitudes. I then need that data to be stored in an external SQL Server db. The problem I'm having is sending that file to a web service. I cannot find any examples on how the class should look that takes in the db file and stores it in a separate SQL Server db. Is this even the way I should be approaching my problem?
A better approach would be to send the actual lat/long data to db via a web service rather than sending the entire db file itself.
Doing it in this way would accomplish several things:
It should be much simpler to implement
You would not need to support SQLite on the server side, just the client
The data "set up" would be immediately available for querying - rather than needing to be extracted from the SQLite db file before it can be used
EDIT: How frequently and how much you upload is entirely up to you. You can make it user-activated or on some time interval and upload the latest data in bulk fashion or one-at-a-time until you're up to date. In either case you would track which data needs to be uploaded with a timestamp.
One simple method for transfering "in bulk" would be to pull the data that you need to save from you SQLite db and put it into a JSON or XML object which would be interpreted on the server as a collection of lat/long data. This would put the whole upload into a single web service call rather than having to loop through your "newest" records and calling a web service for each item.
Rather than sending a database to server you should have a mechanism that can export only data and send it to the database.
You can export the data into the CSV file or any other format and then you can send it to the sever so that server can easily read that CSV and insert that data into the database.
Also you can read your SQLite database and then you can make a structured data like XML, JSON.
then you can connect to the webservice and then send thay structured file to the server.
CSV is the best option if you have much data and you want to send it to the server.

Downloading data/content into Sqlite database

I want users to be able to get additional content from my website which means I will insert the downloaded data into the device's SQLite. I am wondering if I am approaching this the right way..
My current approach is to create a REST web service which returns data in JSON format, parse the JSON and insert it row by row into the Sqlite db on the android device.
Is this the right approach? Will it be too slow if there are many table rows to be inserted at one time? Or is there a way to download another SQlite db and merge it with the local one?
I welcome any suggestion, thank you in advance for your answer.
I works, but you absolutely need to paginate : set a limit to the number of element sent by your rest service.
Another approach would be to download the complete sqlite database file at once, but that requires some tweaks. see http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ (it is about embeding the database from the assets, but the preparation of the database is the same.)
A last point: large amount of insert, as well as downloading data from a server, must to be done in a separate thread or asynctask, from a service (not an activity that can be interrupted), or even better from a SynchronizationAdapter, which is called by the system itself.

Categories

Resources