【发布时间】:2012-07-24 09:17:07
【问题描述】:
我正在查询 CallLog 内容提供程序,需要检测列类型。
在 Honeycomb 和更新版本(API 级别 11+)中,您可以通过调用返回以下类型之一的方法 Cursor.getType(int columnIndex) 来获取列首选数据类型:
- FIELD_TYPE_NULL (0)
- FIELD_TYPE_INTEGER (1)
- FIELD_TYPE_FLOAT (2)
- FIELD_TYPE_STRING (3)
- FIELD_TYPE_BLOB (4)
我如何在Honeycomb
我尝试了以下方法:
for ( int i = 0; i < cursor.getColumnCount(); i++ ) {
int columnType = -1;
try {
cursor.getInt( i );
columnType = Cursor.FIELD_TYPE_INTEGER;
} catch ( Exception ignore ) {
try {
cursor.getString( i );
columnType = Cursor.FIELD_TYPE_STRING;
} catch ( Exception ignore1 ) {
try {
cursor.getFloat( i );
columnType = Cursor.FIELD_TYPE_FLOAT;
} catch ( Exception ignore2 ) {
try {
cursor.getBlob( i );
columnType = Cursor.FIELD_TYPE_BLOB;
} catch ( Exception ignore3 ) {
columnType = Cursor.FIELD_TYPE_NULL;
}
}
}
}
}
但是,不会引发异常。数据始终以您要检查的第一种类型进行转换,在本例中为 getInt()。这意味着,如果列类型为 Integer 但对于所有其他类型为 0,我会得到正确的值。
为什么我不查看文档来检查存储的类型? 这些列因设备制造商而异,并非所有列都记录在案,请参阅此问题:How to handle manufacturer-dependent differences in ContentProviders?
有什么想法吗?
【问题讨论】:
-
你能对 SQLite 的元数据表执行 SELECT 吗?
-
我正在与官方通话记录内容提供商合作。 AFAIK 你不能 rawquery、pragma 或请求这些表的元数据。似乎您已绑定到 ContentResolver.query() 方法...
-
你试过这个技巧吗? stackoverflow.com/questions/6145876/…
-
我认为 hack 不能应用于我的问题,因为我处理的不是原始 SQLite 数据库,而是内容提供者。还是我错过了什么?
-
你真的不知道你的列包含什么吗?
标签: android android-contentprovider android-cursor