android using sqlite as a static data [duplicate] - android

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android pre-filled DB
I want to use SQLite as a static data. By that I mean to preload it with data before application first start. This data is static = no inserts, deletes or updates. I just want to use full power of sql database queries and indexes. Because my SQLite database should have a lot a lot of data I don't want to upset users with long initial loading time.
Is there any way to do it (preloaded, predefined, preinserted database)?

No, sadly this is not possible. A workaround is to copy the database over to the application data or to the SD card (if available) and load it from there, but IMHO this is an ugly solution.
Have you thought about using Java data structures for this data? It should be much faster to preload. If you do not want to use plain Java structures, I would recommend to use JSON or protobuf - whatever works best for you. :-)

Related

Adding large static text data to SQLite database in android [duplicate]

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/

How to store Data for my application

I'm really new to programming apps - so this question might sound a bit strange:
I'm trying to program an app in android studio, where people can upload different things (basically strings and links put together in some kind of "package") and other peoble can then decide what "packages" they want to add inside their app. However after downloading, this data should be stored on their device and not just in the memory of the phone so that they can use it after restarting the app (and also if theres no internet connection). Do you have any idea what would be the best way to store this data both on the phone and in a database and how to synchronize the data on the phone with the selected data from the database. I really dont want to know how to do this exactly but would rather like some basic ideas and maybe you could tell me what kinds of stuff i should learn in order to succeed and what kind of database would be best here (firebase, MYSQL,..)?
Thanks a lot,
Andi
First of all you should decide what DB you are going to use.
In my opinion all RDBMs are good, but using Sqlite in order to achieve best performance on android devices is a good idea.
In your case you need server-side DB and application too.
(Depend on the scenario and framework you use can be different (sql,mysql,PostgreSQL,oracle,...)).
About how to sync local database with server-side you can download new DB from server and replace it with previous one, if you need previous user data you can have 2 different table and update one by downloading it from server, and save id or any identical row from specific package that already saved by user.
These are some question has been already answered in Stackoverflow
java - How to update table in sqlite?
java - SQLite in Android How to update a specific row
Create SQLite database in android
If you are talking about local databases. Go for Realm or look up a good ORM on github (Object relational mapping, you dont have to write SQL queries explicitly) .
I would suggest Realm which is very fast and user friendly.

Is it a good idea to fill a database using a .txt? [duplicate]

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.

Do changes in SQLite automatically get reflected in MySQL? [duplicate]

This question already has answers here:
How to sync SQLite database on Android phone with MySQL database on server?
(5 answers)
Closed 9 years ago.
I am new to using services. In my application I want to run services in the background. I am creating the same database in MySQL server and SQLite. If I change some data in a table in the SQLite database, does it mean it should automatically store the change in the MySQL-server also?
To answer your question: no, it does not automatically reflect the changes made in SQLite into MySQL.
Why? Because you basically have 2 different databases, although they have the same name (and probably structure). In order for them to communicate and get synchronized, you need to implement some kind of communicating mechanism.
As suggested to you by eggyal in an above comment, take a look here, as a solution for this kind of problem is explained there.

Using Rest to store data in Sqlite

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.

Categories

Resources