简体   繁体   中英

how to get a list of rows that have the same value in a column?

I need to get all rows that have the same value "r_c" in "Tname" column from the table of my db.

 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 ;
}

Answering from mobile, sorry if any mistake in the code, but the idea is simple.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM