目前,下面的代码显示数据库中的所有行,但我想将其更改为仅显示最后一列和最新一列。我需要添加和更改什么才能做到这一点?
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;
}
最佳答案
要显示最后一行,请使用以下代码,最新列是什么意思?
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
}
}
关于java - 我怎样才能只显示最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069187/