How do you get the type from a cursor?
Asked Answered
B

2

3

The javadocs indicate that android.database.Cursor has a getType(int) method. However, when I try to call this method, Eclipse gives me an error saying no method exists. What gives?

Brasil answered 26/5, 2011 at 22:30 Comment(1)
Are you sure the import was done correctly?Dachy
G
6

What version of Android are you targeting? The getType() method applies only to v3.0 and later.

EDIT: Assuming you're targeting pre v3.0 versions of Android then a possible 'hack' to discover the type of each column within a table would be to query the sqlite_master table to find the CREATE data.

SELECT sql FROM sqlite_master

It's pretty nasty but if you really need to find the type then you could extend SQLiteCursor and add your own getType(int columnIndex) method.

You could perform a one-off query to the sqlite_master table when the cursor first accesses a table then parse the CREATE statement from the sql column and store the 'types' in an int[].

In the getType(int columnIndex) you would simply return the value of your int[] based on columnindex.

As I said, bit of a hack but it would work.

Guileful answered 26/5, 2011 at 22:45 Comment(2)
Ah,ha! You are correct. I'm targeting v1.5. When looking at the Javadocs, I wasn't filtering by the appropriate API level (duh). Yes, I'll have to extend the cursor class as you suggest or come up with a different solution. What I'm ultimately try to do is dynamically create odata posts using the odata4j library. Another solution would be to just code a per-table solution. Thanks for the help!Brasil
@SBerg413: Glad to be of help. Good luck.Guileful
B
3

As you can store any type of data in (nearly) every SQLite field. Looking up the data type in sqlite_master and by using the pragma command is not a safe method. You can get the type of the fields using a query like this:

select Field1, typeof(Field1), Field2, typeof(Field2) from MyTable

This statement returns both the value of the fields and their types which might / might not change from record to record.

Boneblack answered 27/8, 2011 at 20:33 Comment(2)
The intention was to get the expected value in order to create an oData xml structure to post to a server. So, unfortunately, your solution is problematic. Both previous answers are a better solution.Brasil
In my work with SQLite databases on the Android platform (the aSQLiteManager) I have learned the hard way not to rely on expectations ;-)Boneblack

© 2022 - 2024 — McMap. All rights reserved.