Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ContentValues have a put method that supports Boolean?

The ContentValues class contains a method that allows Booleans to be put into the values collection. AFAIK, SQLite does not contain a native Boolean format that Android could push the boolean values into. So, what magic does Android do behind the scenes to store these values?

Also, why is there no complimentary getBoolean method on a Cursor? To me, this appears to be a pretty awkward design oversight since there seems to be no "safe" way of retrieving a boolean value that was put into the DB via ContentValues. What am I missing?

This question may seem a bit frivolous since I suspect that the boolean's are stored as a 1 or 0 integer, but why would Android commit to developers making that assumption? Its not even documented as far as I am aware.

like image 760
Justin Breitfeller Avatar asked May 02 '11 19:05

Justin Breitfeller


People also ask

What is the purpose of ContentValues class?

This class is used to store a set of values that the ContentResolver can process.

What is ContentValues?

ContentValues is a maplike class that matches a value to a String key. It contains multiple overloaded put methods that enforce type safety. Here is a list of the put methods supported by ContentValues: void put(String key, Byte value) void put(String key, Integer value)


1 Answers

The ContentValues class contains a method that allows Booleans to be put into the values collection. AFAIK, SQLite does not contain a native Boolean format that Android could push the boolean values into. So, what magic does Android do behind the scenes to store these values?

From reading this document, it sounds like the boolean to integer conversion is done by SQLite.

Also, why is there no complimentary getBoolean method on a Cursor? To me, this appears to be a pretty awful design oversight since there seems to be no "safe" way of retrieving a boolean value that was put into the DB via ContentValues. What am I missing?

If you're reading from a cursor, then you know what columns should be returned from the query, so you presumably know the data types of the columns that were requested. I agree that having a getBoolean method would be better, but it's not hard to work around.

like image 125
Erich Douglass Avatar answered Oct 23 '22 12:10

Erich Douglass