为什么ContentValues有一个支持布尔值的put方法?

发布于 2024-11-04 19:18:36 字数 306 浏览 0 评论 0 原文

ContentValues 类包含一个允许将布尔值放入值集合中的方法。 AFAIK,SQLite 不包含 Android 可以将布尔值推入的原生布尔格式。那么,Android 在幕后使用什么魔法来存储这些值呢?

另外,为什么游标上没有免费的 getBoolean 方法?对我来说,这似乎是一个相当尴尬的设计疏忽,因为似乎没有“安全”的方法来检索通过 ContentValues 放入数据库的布尔值。我缺少什么?

这个问题可能看起来有点无聊,因为我怀疑布尔值存储为 1 或 0 整数,但为什么 Android 会要求开发人员做出这样的假设呢?据我所知,它甚至没有记录。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-11-11 19:18:36

ContentValues 类包含一个
允许放置布尔值的方法
进入值集合。 AFAIK,
SQLite 不包含本机
Android 可以推送的布尔格式
的布尔值。所以呢
Android 背后有什么魔力
场景来存储这些值?

从阅读 this 文档来看,听起来布尔到整数的转换是由 SQLite 完成的。

还有,为什么没有免费的
游标上的 getBoolean 方法?大部头书,
这看起来很糟糕
设计监督,因为似乎
不是“安全”的检索方式
放入数据库的布尔值
通过内容值。我错过了什么?

如果您正在从游标中读取数据,那么您就知道应该从查询中返回哪些列,因此您大概知道所请求的列的数据类型。我同意使用 getBoolean 方法会更好,但这并不难解决。

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.

晚雾 2024-11-11 19:18:36

更新

谷歌已经修补了前面提到的错误,尽管在本文发表时尚未实现:

https://code.google.com/p/android/issues/detail?id=232274


值得注意的是,当前的 API 很危险,如果底层发生任何变化,可能会破坏您的应用程序。

此外,ContentValues.getBoolean 有一个主要问题,如果您使用 DatabaseUtils.cursorRowToContentValues 创建 ContentValues,它会将每个字段视为字符串:

< code>values.put(columns[i],cursor.getString(i));

当您通过 ContentValues.getBoolean 检索该字段时,您将始终得到 false:

if (value instanceof CharSequence) {
    return Boolean.valueOf(value.toString());

因为 value 是 " 0”或“1”此转换失败:

private static boolean toBoolean(String name) {
    return ((name != null) && name.equalsIgnoreCase("true"));

因此我强烈建议您创建自己的 getter 和 setter,以便明确定义您的行为。

UPDATE

Google has patched the previously mentioned bug, though it's not implemented yet at the time of this post:

https://code.google.com/p/android/issues/detail?id=232274


It's worth noting that the current API is dangerous and could break your app if anything changes under the hood.

Additionally, ContentValues.getBoolean has a major issue that if you create the ContentValues with DatabaseUtils.cursorRowToContentValues it will treat EVERY field as a string:

values.put(columns[i], cursor.getString(i));

When you then retrieve the field via ContentValues.getBoolean you will ALWAYS get false:

if (value instanceof CharSequence) {
    return Boolean.valueOf(value.toString());

Since value is "0" or "1" this conversion fails:

private static boolean toBoolean(String name) {
    return ((name != null) && name.equalsIgnoreCase("true"));

So I highly recommend that you create your own getter and setter so your behavior is well defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文