【问题标题】:how to get a list of rows that have the same value in a column?如何获取列中具有相同值的行列表?
【发布时间】:2016-07-12 23:40:41
【问题描述】:

我需要从我的数据库表中获取“Tname”列中具有相同值“r_c”的所有行。

 public ArrayList<String> getDb2(String r_c ){
    ArrayList<String> dd = new ArrayList<>();
    String KW = r_c.toString();
    String selection = "Tname " + "like ? ";

    Cursor cursor = db.query("info", null,selection,new String[]{"%"+KW+"%"},null,null,null);
    if(cursor !=null && cursor.moveToFirst()){
        do{
            dd.add(cursor.getString(0)+"\n " + "From :  " +cursor.getString(2)+"\n "+"price : " + cursor.getString(1)+" \n "+"Nots : " + cursor.getString(3));
        }
        while (cursor.moveToNext());
    }
    return dd ;
}

【问题讨论】:

    标签: android mysql database arraylist cursor


    【解决方案1】:

    从手机回答,如果代码中有任何错误,请见谅,但想法很简单。

    Cursor c=db.rawQuery("SELECT * FROM your_table_name WHERE Tname='r_c', null);
    
    if(c.getCount()==0)
        {
            showMessage("Error", "No records found");
            return;
        }
    
    StringBuffer buffer=new StringBuffer();
        while(c.moveToNext())
        {
            buffer.append(c.getString(0)+"\n");
            buffer.append("From: "+c.getString(2)+"\n");
            buffer.append("Price: "+c.getString(1)+"\n");
            buffer.append("Nots: "+c.getString(3)+"\n\n");
        }
    // Displaying all records
        showMessage("Student Details", buffer.toString());
    
    public void showMessage(String title,String message)
    {
    Builder builder=new Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多