我正在尝试使用以下代码从 SQLite 数据库中读取数据:
public List<DBEntry> getAllDBEntrys() {
List<DBEntry> DBEntrys = new ArrayList<DBEntry>();
Cursor cursor = database.query(DatabaseClass.TABLE_APPTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
DBEntry DBEntry = cursorToDBEntry(cursor);
DBEntrys.add(DBEntry);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return DBEntrys;
}
private DBEntry cursorToDBEntry(Cursor cursor) {
DBEntry DBEntry = new DBEntry();
DBEntry.setId(cursor.getLong(0));
DBEntry.setName(cursor.getString(1));
DBEntry.setStartDate(new Date(cursor.getLong(2)));
DBEntry.setDueDate(new Date(cursor.getLong(3)));
DBEntry.SetPriority(cursor.getInt(4));
DBEntry.setDesc(cursor.getString(5));
DBEntry.SetCompletion(cursor.getInt(6)==1?true:false);
return DBEntry;
}
我收到此错误:
01-23 16:35:23.509: E/CursorWindow(14609): Failed to read row 0, column 6 from a CursorWindow which has 1 rows, 6 columns.
数据库已经有一个条目,如 sqlite3 实用程序所示:
sqlite> select * from ThingsToDo;
0|asdf|1358847122203|1359192722202|7|abcd|0
sqlite> .schema
CREATE TABLE ThingsToDo( _id integer primary key autoincrement,name text not null,start_date integer,due_date integer,priority integer,description text, completed integer);
CREATE TABLE android_metadata (locale TEXT);
我认为问题在于 _id 列不计为一列,因为该cursor.getColumnCount()
函数返回 6,而不是当我将文件移至 PC 时 sqlite3 实用程序显示的 7。我怎样才能使这项工作?