简单的SQLite数据库获取Cursor错误

时间:2014-03-31 17:00:01

标签: android sql database cursor

- 编辑 - 解决方案 -

感谢laalto我意识到我忘记更新我的DataSource中的allColumns数组,以在我的SQL查询中显示我的新列:

private String[] allColumns = {ClientDataSQLHelper.COLUMN_ID, ClientDataSQLHelper.COLUMN_CLIENT_NAME,
                        ClientDataSQLHelper.COLUMN_CLIENT_ADDRESS, ClientDataSQLHelper.COLUMN_CLIENT_NUMBER,
                        ClientDataSQLHelper.COLUMN_EMPLOYEE_ID};

- 初始问题 -

所以我试图在Android上创建一个简单的数据库,它将维护客户名称,地址和数字,以及为与客户合作的员工附加员工ID(想想清洁或家庭护理服务) 。我遇到的问题是我的表应该(从我认为我的设计方式)应该有5行,给定以下SQL查询字符串:

private static final String DB_CREATE = "create table " + TABLE_CLIENTS + "(" +
        COLUMN_ID + " integer primary key autoincrement, " +
        COLUMN_EMPLOYEE_ID + " integer, " +
        COLUMN_CLIENT_NAME + " text not null, " +
        COLUMN_CLIENT_ADDRESS + " text not null, " +
        COLUMN_CLIENT_NUMBER + " text not null);";

在我的SQLHelper类中,以及各自的变量:

public static final String TABLE_CLIENTS = "clients";
public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_CLIENT_NAME = "name";
public static final String COLUMN_CLIENT_ADDRESS = "address";
public static final String COLUMN_CLIENT_NUMBER = "number";
public static final String COLUMN_EMPLOYEE_ID = "employee_id";

此查询在我的SQLHelper的Overriden SQL查询中执行:

@Override
public void onCreate(SQLiteDatabase db) {
    Log.d(TAG, "creating database");
    db.execSQL(DB_CREATE);
}

我遇到的问题是,当我尝试实现我的DataSource类时,我的getAllClients()方法:

public List <ClientHolder> getAllClients(){
    List<ClientHolder> clients = new ArrayList<ClientHolder>();

    // Again, without our Cursor, we can't actually point at any of the data we want to
    // work with/manipulate
    Cursor cursor = db.query(SQLHelper.TABLE_CLIENTS, allColumns, 
            null, null, null, null, null);
    cursor.moveToFirst();

    while(!cursor.isAfterLast()){
        ClientHolder client = cursorToClient(cursor);
        clients.add(client);
        cursor.moveToNext();
    }

    cursor.close();
    if(clients.size() == 0){
        return null;
    }
    return clients;
}

利用我的cursorToClient()方法从我的数据库中的光标位置实际获取数据:

private ClientHolder cursorToClient(Cursor cursor){
    ClientHolder client = new ClientHolder();
    long id = cursor.getLong(0);
    String name = cursor.getString(1);
    String address = cursor.getString(2);
    String number = cursor.getString(3);

    Log.d(TAG, "Client " + id + ": " + name + ", " + address + ", " + number);

    int employeeId = cursor.getInt(4);  // THIS IS MY ERROR LINE

    client.setClientID(id);
    client.setClientName(name);
    client.setClientAddress(address);
    client.setClientNumber(number);
    client.setEmployeeID(employeeId);
    return client;
}

在线上整数employeeId = cursor.getInt(4);我收到错误Failed to read row 0, column 4 from a CursorWindow which has 1 rows, 4 columns.但是根据我的SQL创建查询(_id,name,address,number,employee_id)我应该有5列,不是吗?我的日志打印正确,所以到目前为止我的值是正确的,似乎我的SQL的COLUMN_EMPLOYEE_ID + " integer, "部分没有被有效地实现。

1 个答案:

答案 0 :(得分:1)

可能是以下任何一种情况:

  1. allColumns投影只有4列。

  2. 您编辑了表架构,忘记更新数据库文件。请参阅When is SQLiteOpenHelper onCreate() / onUpgrade() run?

  3. 您的更新问题显示原因是1。