Android - Best way to reference data in a database - android

I have an app that uses an sqlite database. Currently I have a database helper class that I call from within my main class to access the database. I then temporarily store the items I retrieved in a cursor. Every time I want to access my database I have to open the db, create a new cursor, query the db and add the results to the cursor, pull the information out of the cursor, close the cursor, close the db, then display to the user.
Now I'm accessing the db helper class every time I want to access the db
I'm creating a new cursor every time
And I'm making sqlite queries.
I was wondering if I should change my code to do something like:
Open DB
Store all data to a cursor
close db
then whenever I want to access the information I can instead just reference the cursor eliminating the need to call the db helper class again.
Or would it be more efficient to instead store the data to an array doing something like:
open db
store data to a cursor
loop through cursor and store data into an array
close cursor
close db
What would the difference be (processing time, and memory use) between these and is there an even more efficient way of doing this that uses the least resources? Is it faster to use an array or a cursor? I don't want to waste cpu time calling unnecessary functions or use more memory storing bloated objects.

In your activity if you want to execute a select query once and you using that data to your activity then you have to make a ArrayList or Hashmap from Cursor and then you have to close your cursor.
But if you have to access a database many times and you are getting a different different results by executing a query then you have to use cursor and fetch data from cursor update to UI and close Cursor.
You are using Helper class so it is the best choice so you don't have to wonder about processing.
when ever you want to fetch data then just make a object of Helper class,Open connection,execute query and return data,fetch data from cursor in your activity,Update UI,close database.
this is the normal way and also it is a good way.

Related

How to create Cursor data without getting data from the DataBase in Android application?

In my Android application, I am using Sqlite DataBase to store the data from the server. I am using ContentProvider and ContentResolver to access the data from the DataBase and using CursorAdapter to bind the data to the ListView. As soon the data is inserted into the DataBase, CursorAdapter will be notified to update the ListView. Also, whenever I scroll the ListView, I get new data from the DataBase table and ListView will be updated. But once I reaches to the end of the table row, I need to fetch the data directly from the server without storing into the DataBase and update it in the ListView. Now as I am using CursorAdapter which accepts Cursor data, how can I bind a new set of data which is not coming from the DataBase? Is is possible to create Cursor data without getting data from the DataBase and use ChangeCursor() method in the ContentProvider to update the ListView? If not, is there any other method to achieve the same?
Is is possible to create Cursor data without getting data from the
DataBase and use ChangeCursor() method in the ContentAdapter to update
the ListView?
Yes, you can create cursor using MatrixCursor. If you will have to merge MatrixCursor with database Cursor use MergeCursor.

Does deleting data from a database affect the cursor storing the data?

I have to move some files from database A to database B.
So, I query the data into cursor from database A, then delete the data in the database A.
After that, I use cursor to insert the the data rows to database B. Are there any problems in this procedure?
Because someone told me that cursor may be only a reference, not really store the data in the cursor object. So, if I delete the database A before I insert the data into database B, there may have something abnormal!
Is there anyone who knows whether cursor is a reference to database or it really saves the data in the object?
Your case will not work. Cursor is a reference to a part of memory. If you deletes the database the data in this part of memory will be also deleted.
You can use cursor but you should delete your old database only after migration. Why do you want to delete old database before inserting results in a new database?
A Cursor can act only as a reference and if you delete your data from the database A before inserting into database B, you would completely lose your data.
A better and more logical approach would be to query the data from database A, insert it into database B and then delete the data from the database A.

do I need to store the cursor for a listview at class level?

My listview uses a cursor via an adaptor. Do I need to keep this cursor variable at a class level for any reason as the Listview adaptor stores it anyway?
Actually i don't understand why you want to do this if you want to access records again then just store them after fetching through cursor and then you can access them any where in your project.
If you keep this cursor variable then you again want to traverse the database in order to retrieve or other operations . But i think make cursor for you function level access records(you can store them in Vector or ArrayList) then use that stored records instead of using cursor.
If you want to tore cursor make it of class level and access it in other classes (but remember the function reinitialize cursor must call first before calling cursor in other classes).
Hope this explanation works for you...

Create a cursor to point to an array

I'm trying to create a search suggestion content provider, and I need to to return its results as a cursor (for the contentprovider API of query).
The thing is, my data isnt saved in a SQLite database but in an array.
I need to create a cursor object which binds to an array, but I haven't seen any sample that does that.
Does anyone know what's the simplest way to bind a cursor to an array?

Is it possible to mock the sqlite3 Cursor in Android when no db or table exists?

I am using a ContentProvider for caching results from a web-service query. It is an HTTP request and the response content is XML. Most of the data is cached, so I simply query the DB, if not found, request from webservice, insert in DB and requery the DB. Thus the response is always a Cursor from SQLiteDatabaseHelper.
I have one result set that is not stored in the DB and since it is 100% transient, but I would like to provide the appearance of it coming from the DB's Cursor. Is there an easy way to do this? For example, if I could project it onto the cursor with a cursor.setValue("string", objectValue) or some other existing implementation.
If not, I will either bypass the DB for this content result, or stuff it into a trivial table that is constantly reused.
Depending on how you use it, it might not be too hard to write your own cursor class. For convenience, derive your class from AbstractCursor class which takes care of a lot of the details for you.
You may also be able to make use of MatrixCursor.

Categories

Resources