insert large data from file into android sqlite database - android

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.

Related

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

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.

Is it OK to do about 100 queries to Android SQLite database to save a data item?

I've been implementing SQLite data storage for my Android translator app.
The situation is: the app receives traslation as a JSON from server, parses it and builds an object tree (50 to 100 objects in average);
Now I need to save it into SQLite DB. I've asked my teacher, and he recommended not to save the whole JSON response but to make a more complex DB structure.
I've done so, but I'm concerned for those 50-100 DB queries, which are done (and, respectively, 50-100 rows created) to save just one dictionary article.
Now I'm wondering what is the best way to store complex object tree in DB or if it is OK to do up to 100 DB queries.
Maybe, nevertheless, the better way is to store the JSON response in DB? The only operation over the dictionary articles I need is to search them, no modification is needed.
It is better to have a proper database structure and normalised data. This will allow you to query your data more efficiently. Also, it will be much easier to understand for other people if they ever develop your code.
You should do all your updates in one transaction and you will be fine with saving 100 records in one go. You can use a structure similar to that one:
SQLiteDatabase db = sqlHelper.getWritableDatabase(); // this returns a database
db.beginTransaction();
try {
// all your updates
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
db.close();
If you use transactions properly, you'll be fine. Do all the writes within a single transaction and commit the transaction when complete.

Keeping a DB updated - Android

With your help, I have successfully created a web page which has its own MySQL DB and then uses a JSON web service to pass the values my Android application.
The next stage is to copy them into a local SQLite DB so the phone does not require an internet connection to view the list.
I have successfully implemented code that creates a table, and inserts values into this table.
The problem is, I don't want it to necessarily re-write the whole DB at a time, which is the current idea to keep it current.
I might have things in the DB deleted or more things added, and want the local DB to reflect this.
What steps can I take to delete things that are no longer present in the MySQL DB in the SQLite one, and add new things?
Cheers
The best way to do this should be to have a date time column in your database and then just fetch the posts that are newer then the last time you fetched from the database, in your android app you could save an date time in your preferences
I'm sorry I missed that you wanted to delete items as well, than this probably isn't the best approach. Are you using the mySQL only for the API? Then you could probably mark posts as deleted inserted odd feeling them.
You can think this approach, parse your JSON response into objects of your data model and then create the content values holding that data model to work with the database in your device.
Once you have the content values, use a content resolver to work with your database and try an update of each data parsed from the JSON response (each content value), update method of the content resolver will return an integer value that tell us the number of rows updated in the database, if such number is 0 means that no data were in the database, so make an insertion, simply.
In that way you first search if the current data is present in the database, if it is, update it, if not, insert it.

JSON file VS SQLite android

I will develop an android application with a lot of data (json files with some rows and CSV for graphics data with a lot of rows) , this data change every 5 minutes and replaces all the previous data (or mostly).
What are the best approaches to design this ? I have 2 options:
Save all the data in a sqlite db, and sync this by a IntentService.
save the data in json and csv files and replace this every 5 minutes.
Which approach will the best performance?
This considering the time to parse the files, sorting data, the download time and the consistency of data.
any other ideas?
PD:I need a cache system too, in case if i don't have internet and I need the previous stored data
Advantages of SQLite:
Changes are ACID
You can make complex requests faster (e.g. "give me only fields A,B from items with (C/D)>E")
I would bet more compact for big data (integers are stored as integers instead of a string of individual digit)
Only one file
You can merge old data with new data easily
You can update current data, even while using it
Concurrency can be handled
Advantages for JSON/CSV:
Easier to debug (plain text)
Faster to make a whole update (copy the new file + delete the old one)
For the original question the whole delete/replace of the data makes JSON/CSV the winner.
However, if the application was to retrieve partial data every 10s and merge/update it with the previous one, SQLite would be a better option.
Sqlite is mostly used when you want data to be saved and used in future. In your case data is changing every 5 minutes so its better to have JSON because every time to make the Database connection store and retrieve after 5 minutes will take some time.
UPDATE:
I also had the same Application in which the data was changing every time. In that case I used Map<K, V> and ArrayList to maintain the values, because as the data is changing everytime I think its not feasible to store the data in Sqlite everytime. It needs much time to perform DB Connection store, retrieve, update the data in Sqlite.
I recommend using JSON or some type of object serialisation unless:
You need ACID compliance for write operations
You need to report against the data which may involve copying the data to an external RDBMS
or
You wish to join those complicit in the overuse / abuse of databases, as commonly seen nowadays
Ideally this should depend on whether you need the previous data, for maybe comparing it with current data and so on. As a thumb rule, I use SQLite when you need the data to be stored and retrieved at a later stage. In case the data is only for display, I would rather keep it in program memory. Mind you this does not involve file operation.
Purpose of JSON and SQLite is completely different from each other
JSON = is used send and receive data between server and client.
SQLite= is used to store data.

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