我怎样才能显示最后一行?

时间:2014-04-14 19:59:17

标签: java android database sqlite

目前,下面的代码显示了数据库中的所有行,但我想将其更改为仅显示最后一列和最新列。我需要添加和更改才能执行此操作?

public String getData() 
{
    // TODO Auto-generated method stub
    String[] columns = new String[] {KEY_ROWID, KEY_FNAME, KEY_SNAME,
    KEY_HOURS, KEY_REG, KEY_CPARK, KEY_TIMESTAMP};
    Cursor  c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
                                          null, null);
    String result = " ";

    int iRow = c.getColumnIndexOrThrow(KEY_ROWID);
    int iFname = c.getColumnIndexOrThrow(KEY_FNAME);
    int iSname = c.getColumnIndexOrThrow(KEY_SNAME);
    int iHours = c.getColumnIndexOrThrow(KEY_HOURS);
    int iReg = c.getColumnIndexOrThrow(KEY_REG);
    int iCpark = c.getColumnIndexOrThrow(KEY_CPARK);
    int iTime = c.getColumnIndexOrThrow(KEY_TIMESTAMP);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow) + " " + c.getString(iFname) + "  
                    " + c.getString(iSname) + " " + c.getString(iHours) + " " +
        c.getString(iReg) + " " + c.getString(iCpark) + " " +  
                    c.getString(iTime) + "\n";
    }
    return result;
}

1 个答案:

答案 0 :(得分:3)

显示最后一行使用以下代码,你对最新的列有什么意义?

public String getData() 
{
    // TODO Auto-generated method stub
    String[] columns = new String[] {KEY_ROWID, KEY_FNAME, KEY_SNAME,
    KEY_HOURS, KEY_REG, KEY_CPARK, KEY_TIMESTAMP};
    Cursor  c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
                                          null, null);
    String result = " ";

    int iRow = c.getColumnIndexOrThrow(KEY_ROWID);
    int iFname = c.getColumnIndexOrThrow(KEY_FNAME);
    int iSname = c.getColumnIndexOrThrow(KEY_SNAME);
    int iHours = c.getColumnIndexOrThrow(KEY_HOURS);
    int iReg = c.getColumnIndexOrThrow(KEY_REG);
    int iCpark = c.getColumnIndexOrThrow(KEY_CPARK);
    int iTime = c.getColumnIndexOrThrow(KEY_TIMESTAMP);

    if (c.moveToLast()) {
        result = result + c.getString(iRow) + " " + c.getString(iFname) + "  
                    " + c.getString(iSname) + " " + c.getString(iHours) + " " +
        c.getString(iReg) + " " + c.getString(iCpark) + " " +  
                    c.getString(iTime) + "\n";
        return result;
    } else {
        return null; //no row selected
    }
}
相关问题