This question already has answers here:
Ship an application with a database
(15 answers)
Closed 6 years ago.
I am developing an app that for the most part presents large static textual information. I intend the app to be offline so no fetching of data from server. As already discussed on many posts I have following options as per my understanding:
Storing it in XML resource file
text file in assets folder
SQLite database
Though initially I decided to go with XML, but for features like proper sorting, filtering and indexing of data SQLite is handy. I read here, here and here about trade-offs between various methods. Now, if I am storing data in SQLite:
From where should I feed data? Won't I have to first store it in a text file or Strings.xml?
If yes, then I would need to parse XML(may be multiple files) to insert it in database tables
and then reading tables to display data in Views, leading to redundancy (as I could directly populate views after parsing XML) and also taking double the size for data(XML and database). Am sure there is nothing in android studio like "feeding database from some resource while generating apk and not bundle up that resource as a part of apk". So my question essentially is how to effectively use SQLite for large static text data? Thanks
(sorry if my description is bit lengthy than necessary)
EDIT: Also there will be no data manipulation while the app is being used.
To be clear, your question is more descriptive..
Here is my view on how to solve this problem without redundant processing:
Here are two scenarios to be handled:
Scenario 1: Network available
Step 1: when fetching data from sever, get the data onto an XML. Now create an AsynTask "ParseXML" which will parse the XML.
Step 2: Now, in the postExecute method of "ParseXML" do two things.
One is to store the parsed data onto SQLite database for offline usage.
Second is directly populate the parsed XML data onto your views as you have already mentioned in your question that there is a way for you take the XML data into account and display it.
I believe this way you will be able to save the time of storing the data onto dB and then display.
Scenario 2: Offline display of data when internet unavailable
As said by you, query the data from SQLite and display onto your UI.
Also to answer your rest part of your question, you are not wasting the memory, as it is the requirement for you to have offline storage as well and SQLite is the efficient for this kind of storage which requires further processing.
I think you Should use Green DAo It is an open source Android ORM.
It is easy to handling data.It is provide facility to access,insert/update and delete data easily.
Read this http://greenrobot.org/greendao/
Related
This question already has answers here:
Ship an application with a database
(15 answers)
Closed 7 years ago.
I am currently learning how to use database in Android. For one of my app, I need to stock 500 string values in order to read them.
Actually to fill my database, I read a .txt file at the database creation with all the string values inside. For each lines of .txt (corresponding to a string value), I create a new data in the database. The thing is I know that accessing a file can be long it is not a very good thing if there are thousands of values.
According to that, I have two questions :
Is it really usefull to create a database ? (because I can directly access the string values by reading the .txt)
Is there a better way to fill the database ? Or a way to create a database already filled before the activity creation ? (because currently, each time you close the activity you close the database and each time you reopen it, it recreate and refill the database.)
How about instead of having .txt have a .csv file with values and use org.apache.commons.csv or super csv for parsing the csv file. I haven't used super csv but it provides support for POJO support which i think can be really handy. This will increase your performance and parsing speed. But it would be based on your situation. i would recommend creating a database if you need to perform SQL queries on your data for eg. joins or nested queries. If you just need to display you can use a CSV file.
i,m making an android app which deals with counter information of bus train and plain in Bangladesh. I want to store those information into sqlite database. But i can't figure it out what will be the best approach to store those huge amount of info in a tabular form. I mean m i need to code all the data into my src file? OR is there any graphical UI (like MySql database) of this database to write the data into table without coding it?
i don't sure that you understand my question. If you not then please ask what point i make confusion. Advance thanks for helping :)
But i can't figure it out what will be the best approach to store those huge amount of info in a tabular form
SQLite
I mean m i need to code all the data into my src file?
Usually, you'd use an existing API to read the data you want from an existing database somewhere (in this case, often the transport department), and then programmatically iterate over it and store it locally
OR is there any graphical UI (like MySql database) of this database to write the data into table without coding it?
MySQL is not a graphical program. It is a database. You get various graphical interfaces for it, but on its own it isn't one.
MySQL doesn't work on Android
You can write a simple script or app easily enough to allow you to enter the data using a UI
I am trying to create an Android dictionary-like application and get slow performance on retrieving the data. Currently, each dictionary entry is stored in a text file (inside Android assets), each file is named as number, so that I can use index to locate, open and read the file's content (simply read out a single line of String). When using ListView to render the output data and reading the file's content inside getView() method, it takes about 3 second to retrieve 10 entries. I just wonder if there are another approaches (using SQLLite, ???) for retrieving and rendering these entries faster. Any recommendations are appreciated.
I would suggest you to use SQLite
Advantages
You can query
You can update definitions easily
Your data is more secure while using database (If you can Encrypt
using AES or similar algorithm it, then it will become more
secure!)
Fetching results is more faster
You can easily populate the results to a ListView
You can see a complete article here
SQLite will definitely make your job easier and make the app work faster. It's also a lot easier to read data; when you're writing data to the DB make sure to use transactions to speed up multiple sequential writes. I probably wouldn't even consider using a text file except for initial data. There are many resources available online such as this tutorial.
I'm creating my first android app that will make use of SQlite. I have zero experience with databases, except for creating a mysql database to use with wordpress...
Edit: After doing some research about rest, I'm still confused about how rest, sqlite, and android dev fit together. My goal is to access a rest-based web service through a url and access certain datasets, then store them in my SQlite database. Then I want to access the contents of the database through my java program, and use them accordingly.
The datasets can be downloaded individually in CSV format, but because I will be using so many of them, I don't want to go through every line individually and store them in the database. I'm hoping there's a more efficient way to store these datasets in the database.
My main questions are:
How can I copy the XML contents of a webpage from a url into my sqlite database? Can I do this with my java program, through the sqlite database, or a java library?
Do I only need to copy the contents of the webpages from the url into the sqlite database one time? If so, what can I do if any information is changed in the datasets?
You first need a schema for your sqllite DB. That schema should map to the objects behind the web service. For e.g, you need a Person table in your DB if there is a Person entity on the web. It depends on what all you want to capture.
When you are done designing the schema, you should start writing the code that help you create & manage DB on android. This is done with the help of SQLiteOpenHelper class:
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
If you need to keep the DB synce'd with the data on the cloud (web services), you should implement sync. Android provides a very efficient sync framework.
Also, do watch this video from Android engineers explaining the best practices: http://www.youtube.com/watch?v=xHXn3Kg2IQE
Note, to actually fetch the data from the web service you would use UrlConnection API:
http://developer.android.com/reference/java/net/URLConnection.html
This sample probably captures most of it.
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html
In terms of reading CSV files, there are some good resources here:
Can you recommend a Java library for reading (and possibly writing) CSV files?
Once you have read each CSV line into an object, then you can turn around and persist it to the database. I'm the author of ORMLite so I'll talk about using it. I don't believe there is a hibernate port for Android.
There are a number of Android examples to help you to get up to speed with ORMLite. Also some good tutorials. If you want to write a number of rows at once then I'd recommend using the batch tasks ORMLite feature. For information, see the discussion about creating lists of objects on the mailing list.
I can answer your first question about " I'm not sure how to add them efficiently"?
yes, SQlite is very powerful and intelligent, you can add thousand of records in one transaction, just like traditional database, It significantly improve performance.
about second question, as my understanding, because CVS file is very simple, so you can download and analyze it by yourself.
I'm making a game to run on android. So I want to store player names and the winner, which I will also list up in a view. What is the best way to do this, use a database or write to a file (if so, what type, xml?). I have to be able to add data after every completed game, and the size won't be so large. What would be the best solution?
IMO, using a SQLite database would be the most straightforward. You don't have to worry about the xml parsing that goes along with an xml file. Additionally, the data your storing seems to have a natural relationship that would be conducive to a SQLite schema. For more information about how to use SQLite in Android, see the data storage documentation here.
SQLite is great if you have database concerns and want that sort of data lookup.
However, if you really want to do it Simply with XML then you can in Android.