Search code examples
androidsqlitecursorsqliteopenhelper

query fail getting data from multiple columns using WHERE clause in android


I've tried the following code to return multiple column values with where clause in android

public Cursor getData(String text) {
    SQLiteDatabase db = this.getWritableDatabase();
    String[] columns = new String[]{"Client","Action","Value","Period"};
    Cursor cr = db.query("phrase_table",columns,"phrase = ?",new String[]{text},null,null,null);
    return cr;
}

However, the cursor is returning only the "Client" column value. Here's the code for cursor to string:

Cursor c = vdb.getData(text);
    ArrayList<String> line= new ArrayList<>();
    if (c.moveToFirst()) {
        do{
            line.add(c.getString(0));
        }while(c.moveToNext());
    }
    String resc="";
    for(String a:line){
        resc += a;
    }
    Toast.makeText(this,resc,Toast.LENGTH_LONG).show();

tried this one too:

Cursor c = vdb.getData(text);
    String line = "";
    if (c.moveToFirst()) {
        do{
            line += c.getString(0);
        }while(c.moveToNext());
    }

    Toast.makeText(this,resc,Toast.LENGTH_LONG).show();

But the result is the same.


Solution

  • You are fetching single data from cursor "Client", update your code for other

    if (c.moveToFirst()) {
      do{
        line += c.getString(0);
        line += c.getString(1);
        line += c.getString(2);
        line += c.getString(3);
      }while(c.moveToNext());
    }