简体   繁体   中英

How to get value from each column of an sqlite record?

My sql should return only one record that contains three columns. Funcioando is properly since I am debugging the code to better understand my problem. I need to get the value of each column to move to a ClienteVO class. But he brings the three columns but the first picks in the second when will the error Can1t read row # 0, col # -1 from CursorWindow I checked debugging and column names are correct and the record returns.

SQLiteDatabase db = new DB(ctx).getReadableDatabase();

String sql = String.format("select limite,valor,nome from limite " +
            " INNER JOIN cliente ON cliente.id = limite.cliente_id " +
            "where cliente.id=1");

Cursor rs = db.rawQuery( sql, null);
ClienteVO vo = null;

if (rs != null){
    vo = new ClienteVO();
    rs.moveToFirst();

    vo.setLimite(rs.getFloat(rs.getColumnIndex("limite")));
    vo.setValor(rs.getFloat(rs.getColumnIndex("valor")));
    vo.setNome(rs.getString(rs.getColumnIndex("nome")));
}

Then read about researching iterate over the columns. And he traverses three columns normally. I wonder if it's the only way to get these values and what is the best way to get the attributes.

Cursor rs = db.rawQuery( sql, null);        
ClienteVO vo = null;

if (rs != null){
    vo = new ClienteVO();
    rs.moveToFirst();

    for (Integer i=0; i <= rs.getColumnNames().length; i++ ){
       //How can I get the values of clean way
    }
}
    Cursor rs = db.rawQuery( sql, null);

    ClienteVO vo = null;

    vo = new ClienteVO();
    if (rs.moveToFirst()) {
            vo.setLimite(rs.getFloat(0));
            vo.setValor(rs.getFloat(1));
            vo.setNome(rs.getString(2));
    }

You can write this.

   Cursor rs = db.rawQuery( sql, null);

Above you have sql variable. What does it contain?

Okay, you should use next

if (rs.moveToFirst()) {//}

to check if your cursor has at least one row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM