sqlite queries

by tmoriarty » Tue, 01 Jul 2008 13:33:49 GMT

Sponsored Links
I recently ran into a situation querying a database table that I found
odd.  My table had the following fields. (_id (PRIMARY KEY), path,
title, artist, album, genre).  I was writting a querry that would
reurn all of the distict aritists, albums, or genres to an activity
for display to a ListView.  I was using the query method which returns
a Cursor.  I found that I had to include _id in the Cursor or it would
error out once the Cursor was used in a CursorAdapter.  This forced me
to write a slightly more copmplex querry using a GROUP BY and
uitilizing the MAX() function to return an _id that I didn't really
want.  I believe I understand the reason why primary key must be
present, so you can uniqyuly identify a row in the Cursor to allow you
to make changes to the undelying database through the Cursor.
However, why force a hard coded _id.  This seems to be enforcing a
naming question on the primary key. This doesn't seem like good
practice.  Can anyone shed some light onto why this is designed this
way, and what approach people usually take when they want data in a
Cursor for  ListView display and are unconcerned with the primary key
or changing the database in anyway through the Cursor.  Thanks.

sqlite queries

by Mark Murphy » Tue, 01 Jul 2008 13:49:39 GMT


Could you provide us with the text of the error message? That might help 
us shed some light on the situation.

Mark Murphy (a Commons Guy)
Warescription: All titles, revisions, & ebook formats, just $35/year


Sponsored Links

sqlite queries

by tmoriarty » Wed, 02 Jul 2008 12:14:34 GMT

 The error message reads as follows:
An error has occurred in com.android.mp3player.
Unable to start activity
java.lang.IlleaglStateException: Unable to get field slot.

This error only occurs when I return A cursor from the querry without
_id in it.  The documentation for CursorAdapter says it needs _id to
work properly.  If I understand this correctly it probably needs the
primary key so it can commit changes to the Cursor back to the
database.  However, I don't understand why they force the name to be
_id.  I guess that is the root of my question.  If I am not
understanding this correctly I would appreciate any insight.  Thanks.


sqlite queries

by Mark Murphy » Wed, 02 Jul 2008 12:34:00 GMT


Your analysis is probably correct. I was thinking I didn't use _id in my 
TourIt sample project, but it turns out I did. And, if you want a full 
read-write Cursor, you may not have a choice.

But, if you are merely looking to display the results of a query, I can 
think of two workarounds.

You could try renaming your existing primary key to _id in the results 
via appropriate SQL and rawQuery():

"SELECT MyKey AS _id, Other, Columns, Here FROM MyTable WHERE ..."

I haven't tried that, but I don't know how Android would know the 
difference between a "real" _id column and a renamed one.

Or, you could pour the results of your query into a List and use 
ArrayAdapter rather than SimpleCursorAdapter. This wouldn't be great for 
a large result set, of course.

Mark Murphy (a Commons Guy)
Warescription: All titles, revisions, & ebook formats, just $35/year


Other Threads

1. Location/GPS and SqlLite help needed

I have two applications that I'm trying to finish and I need some
help.  I'm looking for two good android developers who can help me
finish my applications or who have Intents or Activities that will
enable my applications to use Location based awareness and import/
query a large amount of data into SqlLite.  I am swamped with two
other projects and I need help getting these applications finished.  I
have capital to pay these two developers, so if anyone is interested,
please let me know.  This is a work from home opportunity.

Please contact me if you are interested with your rates.


2. apk install error

when i try to install the apk in emulator i'm getting this error... Y

./adb install /home/.../tools/mapdemo/bin/mapact-debug.apk
* daemon not running. starting it now *
* daemon started successfully *
56 KB/s (4703 bytes in 0.081s)
    pkg: /data/local/tmp/mapact-debug.apk

with regards


3. Returning files through a Cursor

4. Can I leave some info after my app is uninstalled?

5. A way to get all apps and their activities?

6. Changing a Back button press to Home button press

7. KeyGuard customization