简体   繁体   English

我如何只显示最后一行?

[英]How can I only show the last row?

At the moment the below code displays all the rows in the database but I want to change it to only show the last and latest column. 目前,以下代码显示了数据库中的所有行,但我想更改它以仅显示最后一列和最新一列。 What do I need to add and change to do this? 为此,我需要添加和更改什么?

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;
}

to show the last row use the following code, what do you mean with the latest columns? 要显示最后一行,请使用以下代码,您对最新的列表示什么意思?

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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