0

Basically I have this:

Cursor cur = ...
for (int i = 0; i < cur.getColumnCount(); i++) {
     String name = cur.getColumnName(i);
     Log.d("dao",name);
     int type = cur.getType(i);

... and getting in the getType() call the above Exception. The column name is logged correct.

ERROR AndroidRuntime Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0

4

1 回答 1

1

这是 SQLite 数据库的一个特性,它使用动态类型。也检查一下。你为什么不使用:

Cursor cur = ...

if (cursor != null) {
    while (cursor.moveToNext()) {
        String name = cur.getColumnName(i);
        int type = cur.getType(i);
        ...
    }
}

这样,如果存在行,您只询问类型。

如果您需要独立于任何结果的列类型,您可以使用:

String tableName = "...";
Cursor cursor = rawQuery("pragma table_info(" +tableName +")");
String type = getString(0);
String name = getString(1);

希望这会有所帮助......干杯!

于 2013-04-26T19:26:45.157 回答