查询无法使用android中的WHERE子句从多列获取数据

时间:2017-06-29 13:40:02

标签: android sqlite cursor sqliteopenhelper

我尝试了以下代码,使用android

中的where子句返回多个列值
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;
}

但是,光标仅返回“Client”列值。这是光标到字符串的代码:

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();

也试过这个:

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();

但结果是一样的。

1 个答案:

答案 0 :(得分:3)

您正从光标“客户端”获取单个数据,更新其他

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