I am building an android application with a database that contains more than 20,000 entries.
When i retrieve data from the database, especially when searching for data, it seems to be working slow.
Especially, when i search data based on an editText. Every time editText TextChange(), i query :
Select * from mytable where data='mydata'
And it runs slowly.
I really don't know how to make it work faster.
Hope anyone can help me!
You can create an index on just that column to make selection work faster. If your column is the second one in an existing index, this index cannot be used most efficiently.
CREATE INDEX idx_mytable_word ON mytable(word)
May be you initialize db connection every time?
Try to add pagination. it's make your app faster.
Related
Suppose, In my app I have a sqlite table that can contain at most 20 row. Each row has 2 column(id, name). Where I frequently need to search by Id to get Name. For this frequent need I have two solution:
Solution 1: Get rows in a arraylist<model> and then find name from array.
Solution 2: Every time search on sqlite table.
Now please give your opinion which one is better?
Remember again, I need this search in my recycleView item, so it call so frequently.
Thanks
I don't really get what is your real intent, but if your task is to search by id often, I would use
LongSparseArray<String> idsToNames; // or LongSparseArray<Model>
Which will map primitive long to Strings in a more memory-efficient way than Map and will have a better performance than ArrayList when searching.
The advantage over querying SQLite here is that you can do it in a blocking manner instead of having to query database on a background thread every time the lookup runs.
The disadvantage is that whenever data changes in SQLite, you will have to rebuild your idsToNames map. Also, if the number of entries in SQLite will eventually grow, you will end up in a large collection. So I would recommend this approach only if the updates to the database during this session will not happen, and if the data size is always predictable or fixed.
I have 4 EditTexts in dataBase
I delete the second one
The database is numbered 1,3,4
I now want it to be renumbered as 1,2,3.
I looked at many solutions
But I could not find a complete answer..
I do not know what should be in writing
I tried the following code .
But it did not happen
db.delete("SQLITE_SEQUENCE","NAME = ?",new String[]{TABLE_NAME});
Does anyone have the full answer ?
In general, this is poor design for a database. An ID should be consistent for a piece of data, regardless of its peers--it should not fluctuate just because another row was deleted.
That being said, you could always use an UPDATE statement to SET ID = ID-1 WHERE ID > (the ID of the row you deleted). This would work. I don't recommend this approach, but you can.
You can recover this space by executing the SQLite VACUUM command.
Call db.execSQL("VACUUM") after delete
I am currently building a database recording events on the phone, but as I don't want to make this a huge database, 100 events are more than enough.
This will keep my database light en efficient.
Unfortunately, I don't see a way to limit the number of rows other than
String sql = "DELETE FROM myTable WHERE _id <= "+limitId;
and I could run this code when the user launch/leaver the app, but I am expecting a better way to achieve this
Is there a more convenient way to achieve this?
If you are using a ContentProvider, you can implement your DELETE in onInsert, deleting a single row on every insert of a single row:
String sql = "DELETE FROM myTable WHERE _id IN (SELECT min(_id) FROM myTable)";
I guess you mean limit to the 100 newest events ?
if so, there is no better way to do it as you did: checking for entries on every insert and delete old entries if necessary.
It's just a matter of taste how or where you do your check, as flx mentioned you could do it in the ContentProvider or as you probably did in the BroadcastReceiver or Service where you actually add the new row. You could also set up a Trigger on your table, but the main idea remains the same. Here a link if you're interested in triggers:
http://www.tutorialspoint.com/sqlite/sqlite_triggers.htm
I'm relatively new to Android but I just cant google this. I have following situation:
quite large SQL db on android (need to select and load about 2000 records to ListActivity)
I use SimpleCursorAdapter so far BUT... it doesn't allow me to load data asynchronously with AsyncTask (SimpleCursorAdapter has no "add()" as e.g. ArrayAdapter does)
I know how to make it work with ArrayAdapter but then I lose the ID attribute every time the time is clicked and I want to do it the "clean" way and keep the id (not save it some place hidden)
===> For now user has to wait till all db output is parsed into GUI, it takes some time. How can I fix it to make it run faster ? I need something like SimpleCursorAdapter.add(item) or extend it but not sure ...
thnx
You should consider having some pagination mechanism, not loading everything in an ArrayAdapter but better, returning a simpleCursorAdapter with just a subset of size N of your records. When the user will reach the last row, display a button to increase N and refetch the data from your database.
Confused beginner here.
I'm extending the functionality of the android notepad tutorial program. I can successfully get the data from the sql to display in a list view and I can use the findNote function to get an individual note.
What I want to be able to do is extract an individual note AND all following notes in the table. I'd be happy with some way to iterate through the remaining notes (a puzzle for me because the rowIds are not sequential) but would also settle for designing a query that... I don't know, returns a String[] with the item with the matching id at position 0 and all subsequent items later in the array.
I'm sure there are a thousand ways to do this, I'm willing to take almost any of them. Please let me know if I need to clarify further.
Just looking at the NotesDbAdapter class in that tutorial, there doesn't appear to be any timestamp captured with the notes. You would need to extend the table structure to include this and update the createNote and updateNote methods to set that accordingly. Then you can write your SQL based upon that timestamp.
John