使用rawQuery访问Android中的记录,然后显示

时间:2014-03-02 22:46:12

标签: android sqlite

我正在使用几个rawQueries来解析Android中的表中的数据。下面的代码工作正常,并返回表中最低的rowid。

public void firstRecord(View v){
    Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE rowid = (SELECT MIN(rowid) FROM surveyDB)",null);
    c.moveToFirst();
    szList.add(c.getString(0));

    Toast.makeText(getApplicationContext(), "Sucessful Event. szRowid is: " +szList +".", Toast.LENGTH_LONG).show();
}

我有两个问题,它们都是非常基本的:1)扩展上述代码以创建语言以捕获该表中该特定rowid处的其他列的内容的最佳方法是什么(rowid,sampler,物种,地方),并在我的申请中显示?也许是这样的事情:

((EditText)findViewById(R.id.edSpecies)).setText("");

.setText()

中使用正确的引用替换“”

2 个答案:

答案 0 :(得分:0)

String TABLE_SURVEY = "surveyDB";
String COL_ROW_ID = "rowid";
String COL_SAMPLER = "sampler";
String COL_SPECIES = "species";
String COL_PLACE = "place";

public ArrayList<SurveyRecord> getSurveyRecords()
{
     ArrayList<SurveyRecord> records = new ArrayList<SurveyRecord>();
     String query = "SELECT * FROM " + TABLE_SURVEY;
     query += " WHERE " + COL_ROW_ID = " SELECT MIN ("
     query += COL_ROW_ID + ") FROM " + TABLE_SURVEY;
     Cursor c = db.rawQuery(query,null);
     if(Cursor.moveToFirst())
     {
          do{
              String sampler = c.getString(cursor.getColumnIndex(COL_SAMPLER));
              String species= c.getString(cursor.getColumnIndex(COL_SPECIES));
              String place = c.getString(cursor.getColumnIndex(COL_PLACE));
              String rowId = c.getString(cursor.getColumnIndex(COL_ROW_ID));
              records.add(new (rowId,species,place,sampler));
            }while(c.moveToNext())
    }
    c.close();
}

public class SurveyRecord{
         String mRowId;
         String mSpecies;
         String mPlace;
         String mSampler;

         public SurveyRecord(String rowId,String species,String place,String sampler)
          {
             this.mRowId = rowId;
             this.mSpecies = species;
             this.mPlace = place;
             this.mSampler = sampler;
          }

}

答案 1 :(得分:0)

//Goes to the first record in the dataset
public void firstRecord(View v){
    Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE rowid = (SELECT MIN(rowid) FROM surveyDB)",null);
    c.moveToFirst();    
    ((EditText)findViewById(R.id.edRowid))  
    .setText(c.getString(0));
    ((EditText)findViewById(R.id.edSpecies))  
    .setText(c.getString(1));
    ((EditText)findViewById(R.id.edArea))  
    .setText(c.getString(2));   
    ((EditText)findViewById(R.id.edSampler))  
    .setText(c.getString(3));
}