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.
Related
I plan on displaying dates and times in an android application but I am stuck on how I would implement this.
My Idea:
At the beginning of every month, a new calendar is downloaded. This information is displayed within the app and no internet connection is necessary for the rest of the app usage.
The information is in the form of an html table. How can I import this table, and display it within the App (also allowing the user to view only certain information in the table (querying the table))
Do I have to import this table into an sqlite database? or is there a less complicated way of doing this. I would like to limit the number of times the user has to access the internet, therefore it would be necessary to store the html table within the app itself.
How about save the table as an xml file? With the xml information you can query, or handle the data as array or list
Can you create a way to access your data in a more "friendly-way"? For example you could create an endpoint online where the app will send a request, download the JSON and save that in a internal database (SQLite, check this if you don't know how).
Or, the ugly way would be to parse the table in the device using Jsoup (which you tagged). Then it's the same, you should save it in a database and read the data from there.
By the way, I would not store directly the html table, because then you will need to parse it every time you need something (p.s it will be slow)
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.
I have user details table which contains thousands of records and i want to store this into sqlite db so that i can access it any time. But i'm not sure that sqlite db will handle thousands of records using cursor.
dont fetch all the record at once. first fetch like 100 or 200 or 500 records. then detect when the user is at bottom of the list(lets say) then fetch another 100 or more records or simply you can add button at the bottom title with "load more" then fetch more records. dont get all the records at once. when user required records then fetch. like this it will not make any problem.
You don't give any detail about the problem that you try to solve. So every answer of your question will be vague.
If you show part of the data in db, implementing paging mechanism will be better.
If you interpret the data in db, maybe storing these data in mobile storage is wrong choice and maybe it is time to move server and do what is required in that server and provide required data to android over network.
I hope it is helpful for you
android sqlite can handle very big db no need to worry but you must see to that your app should not load so slow and feel unpleasant to user
you can answer your question using developers guide
best place to study in detail
link:http://developer.android.com/reference/android/database/sqlite/SQLiteCursor.html
You can not fetch and add records at a time as there because SqlStatement class is not a thread safe class,it can give you error. So before mdoing insert and fetch you should make it synchronize .
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.
We've got an android app and an iPhone app (same functionality) that use sqlite for local data storage. The apps initially come with no data, then on the first run they receive data from a remote server and store it in a sqlite database. The sqlite database is created by the server and the apps download it as one file, which is then used buy the apps. The database file is not very large by today's standards, but not a tiny one either - about 5-6 MB.
Now, once in a while, the apps need to refresh the data from the server. There a few approaches I can think of:
Download a new full database from the server and replace the existing one. This one sounds like the simplest way to deal with the problem were it not for a repeated 5-6 MB downloads. The apps do prompt the user whether they want to download the updates, so this may not be too much of a problem.
Download a delta database from the server, containing only the new/modified records and in some form information about what records to delete. This would lead to a much smaller download size, but the work on the client side is more complicated. I would need to read one database and, based on what is read, update another one. To the best of my knowledge, there's not way with sqlite to do something like insert into db1.table1 (select * from db2.table1) where db1 and db2 are two sqlite databases containing table1 of the same structure. (The full sqlite database contains about 10 tables with the largest one probably containing about 500 records or so.)
Download delta of the data in some other format (json, xml, etc.) and use this info to update the database in the app. Same as before: not to much problem on the server side, smaller download size than the full database, but quite a painful process to do the updates.
Which of the three approaches you recommend? Or maybe there's yet another way that I missed?
Many thanks in advance.
After much considerations and tries-and-errors, I went for a combination of options (2) and (3).
If no data is present at all, then the app downloads a full database file from the server.
If data is present and an update is required, the app downloads some database from the server. And checks the content of a particular value in a particular table. That value will state whether the new database is to replace the original or whether it contains deletions/updates/inserts
This turns out to be the fastest way (performance-wise) and leaves all the heavy lifting (determining whether to put everything into one database or just an update) to the server. Further, with this approach, if I need to modify the algorithm to, say, always download the full database, it would only be a change on the server without the need to re-compile and re-distribute the app.
Is there a way you can have a JSON field for each of the tables? For instance, if you got a table named users, have a column named "json" that stores the JSON for each of the users. In essence, it would contain the information the rest of the fields have.
So when you download the delta in JSON, all you got to do is insert the JSON's into the tables.
Of course with this method, you will need to do additional work in parsing the JSON and creating the model/object from it, but it's just an extra 3-4 small steps.
I will recommend approach 3, because app will download the json file more fast and local db will be updated more easily avoid overhead of more internet usages.
Just create a empty db initially according to server db and then regularly updated the same by fetching json