Search code examples
androidandroid-sqlite

Android(sqlite) How do I view a data by using where clause?


I'm making an application that requires you to input an ID, if there is a data stored in that id, it will show via alertdialog. my problem is when I click [View entered id no.], if there is a data, nothing happens but when I click [View all data] every stored id and info will show up.

my DatabaseHelper query: (COL_1) is the id

public Cursor getOneData(String id){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor result = db.rawQuery("select * from " +TABLE_NAME+ " where " +COL_1+ " = ? ",new String[]{id});
        return result;
    }

my View button code: (txtNum is EditText for id)


        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor res = myDb.getOneData(txtNum.getText().toString());
                if(res.getCount()==0){
                    showMessage("Error","Nothing found");
                    return;
                }
                StringBuffer buffer = new StringBuffer();
                while(res.moveToNext()){
                    buffer.append("ID: "+ res.getString(0)+"\n");
                    buffer.append("Name: "+ res.getString(1)+"\n");
                    buffer.append("Surname: "+ res.getString(2)+"\n");
                    buffer.append("Marks: "+ res.getString(3)+"\n\n");
                }
            }
        });

when view button is clicked, i expect the output to show the information on the entered id no.


Solution

  • In short you tell it to do nothing when a row is extracted, all you do is get the values into the String buffer object call buffer. You do nothing with that data before returning and hence the extracted data is lost as the buffer object no longer exists.

    If you did (as an example) :

        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor res = myDb.getOneData(txtNum.getText().toString());
                if(res.getCount()==0){
                    showMessage("Error","Nothing found");
                    return;
                }
                StringBuffer buffer = new StringBuffer();
                while(res.moveToNext()){
                    buffer.append("ID: "+ res.getString(0)+"\n");
                    buffer.append("Name: "+ res.getString(1)+"\n");
                    buffer.append("Surname: "+ res.getString(2)+"\n");
                    buffer.append("Marks: "+ res.getString(3)+"\n\n");
                }
                Toast.makeText(v.getContext(),"Here is my data:- " + buffer.toString(),Toast.LENGTH_SHORT).show(); //<<<<<<<<<< ADDED
            }
        });
    

    Then for a brief period the extracted data would be shown.

    You would likely want to do something else with the extracted data, the above is just a demonstration that the data will be extracted (assuming that there are 4 columns extracted by the query).

    What else you do and how you do it would be dependant upon code that does not form part of the question asked, so if you are having problems with that aspect then you should ask another question.