I'm thinking of making an app that involves, using a bit of data that is not too large, putting it into an SQLiteDatabase and doing simple operations with the data such as put them into a ListView or display a certain piece of data etc...
So I don't really know that best way of doing this so I've got a few questions to ask.
Is it wise to store my data in a file in my assets folder then extract the data from the file and put it into the SQLiteDatabase?
Is it better to just have the data hard-coded into execute statements when I create my Database?
Should I be using an AsyncTask to populate my SQLiteDatabase with the data I have?
If question 3 is yes, how do I run my Activity such that if I were to simply display the data in my database (in a textview), that the method I call from my Database class that helps me display the data, does so only when the database has been populated (when the AsyncTask has finished).
I hope these questions are clear enough!
Both 1 & 2 approaches work flawlessly.
If you want to take approach #1 I really really recommend using Jeff Gifelt's Sqllite asset helper. I found quite easy generating a sqllite db outside the app and embedding it.
Generally speaking, the SQLiteOpenHelper's onCreate method is created the first time you try to read / write to your db, which is something you always should be doing in another thread. The doc says:
The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.
So the answer is: you should not bother about when the db is created, what you should care about is reading / writing to it from a background thread / asynctask.
Related
In my android app i am performing sql data extensively where i need to download the data from server , store it into database and populating it on the ListView.
moreover, i need to perform the database search and filter the data in a ListView too.
so far in my past projects i have used simple method to manipulate database like manually opening & closing database , getting data from database using cursor and storing it in a array list and populating over ListView.
after exploring i came across the ContentResolver and LoaderManager and tried few samples too but i am not able to decide what to use where i need to perform more database operation with search in a ListView.
I would even love to know in which scenario what approach is preferable.
i am waiting for commonsware comments too ;)
ContentResolver/ContentProvider is meant for providing data to other applications. If you aren't going to do that, then using it is clunky and overkill. It really doesn't provide any value, and even Google's own docs suggest not using it for inside of your app.
LoaderManager- meh. The idea of a Loader is useful, and you're probably already using it- a loader is just the idea of reading large amounts of data from your DB on an AsyncTask. LoaderManager will provide some efficiency gains if you're being restarted due to configuration changes, but there's other ways to achieve them. It isn't a bad thing to use, but it will save you minimal to no work- you still have to write a custom CursorLoader to make your db call, and you'll have some code to manage the loader manager. You aren't wrong to use it, I'm just not convinced of its value.
I'm creating an Android app that will download XML data from my website, parsed xml data will be inserted into 5 separate SQL Lite tables. I have successfully logged into my website, got the XML data & parsed. I not sure how to layout the database code for multiple tables(opening, closing, inserting, selecting).
Also I would like (is this possible) these tables to stay on the device if the user needs to logout and come back to the app at a later time.
Any help or direction would be appreciated.
Thanks
Question title says "multiple databases", body says "multiple tables". I take it to mean "multiple tables".
Just use SQLiteOpenHelper. Create all the tables in onCreate(). Do the opening/closing and CRUD operations the same way you would do on a SQLiteOpenHelper hosting a single table.
Use a search engine to find tutorials about SQLiteOpenHelper in case you need them.
I'm new to android. I want to ask about SQLite database lifecycle. Basicaly, I want to include the insert data method and viewing data method in the main class (which basicaly called 1st when we open the app). Then I'm going to make some update to the database in another activity. What I want to ask is, when I'm going to open the app for the next time, which data that will be showed? Is it the 1st data that I inserted with insert method OR the updated one?
I don't understand exactly your question. So I hope this will answer it. There are 4 operations, you should implement: create, read, update, delete. For example when you have a contact table with id, name, phone number then you can create (John, 012345). If you then call the read method with id 1, you will get (John, 012345). But if you update this before you read it, you obviously get the updated item - the database didn't delete anything when you close the activity. I highly recommend you to create a new class to handle those crud-operations, because you will get crazy, when you wanna change the update operation and must search it in every single line from your app. I also recommend you to read this blog. It saved me a lot of time:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
As far as I understand your question,
my answer is -> You should see the updated data, because Db is same throughout the app.
The data that will be shown is the updated one , because SQL operations are immediate. It is very unlikely that onUpdate() will not update the database until and unless it encounters any error. you can see that itself using a CursorLoader attached with your cursor after running a query command. This can be done in an activity just displaying the contents of the database.
For database operations, It is beneficial to use Content Providers cause then you can extend your apps to widgets, share data with other apps etc.
In terms of storing data in Android, would it be more efficient to use a large ArrayList or setup an SQLite database? I have ~9000 bus stops and stop names (both in String) that I need to store and I was wondering which method would be the quickest or most efficient.
An ArrayList is probably a very bad idea. I assume you want the data to be persistent, so if your user kills your app your data will be lost if you use an ArrayList. A database is persistent(unless the user clears the cache of the app). So I would highly recommend using a database over an ArrayList.
If your data does not change then you could probably have it read on startup and kept in memory while the App runs. For example, having a .json file with all the stops, read it on startup, put the data in a HashMap or something that is easy to use since you will probably lookup stuff. It would probably work fine with ~9000 entries.
If you are going to add or update information about the stops during usage then the SQLite is the better choice though.
1.) Store and retrieve your data from a SQLite DB since you want persistance. And since you say you have 9k+ rows a simple select will give you everything at once and you can easily filter the data as well if you need to
2.) At startup, put all your data into efficient memory structures like HashMaps(or in your case arraylists) and reference them throughout the app. This way you'll only do one time access.
3.) When in doubt build a DB. No harm, more efficient and easier to handle than files
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.