Isuru
Isuru

Reputation: 161

How to retrieve whole Row of data in Sqlite

I want to retrieve a whole row at a time in sqlite database. All of them are strings. I wrote a method. If anyone could check the error of this. Thanks

public ArrayList<Integer> queueAll_row(){
        String[] columns = new String[]{Key_row_ID,Key_Customer_name,Key_customer_nic,Key_roof,Key_floor,Key_walls,Key_toilets,Key_No_Rooms,Key_electricity,Key_drinkingWater,Key_status,Key_ownership
                ,Key_hvBankAcc,Key_loansOfOtherBnks,Key_current_No_Emp,Key_new_Emp,Key_income_source1,Key_income_source2,Key_income_source3};
        Cursor cursor = ourDb.query(DB_Table, columns, 
                null, null, null, null, null);

        ArrayList<Integer> values = new ArrayList<Integer>();
        cursor.moveToFirst();
        while (cursor.moveToNext()) {
            values.add(cursor.getInt(0));
        }

        return values;
    }

Upvotes: 0

Views: 2462

Answers (3)

nurealam11
nurealam11

Reputation: 537

Please try this, I hope its help you.

public ArrayList<ProjectModel> getprojectName() {
    ArrayList<ProjectModel> values = new ArrayList<ProjectModel>();
    String query = "SELECT * from project";

    cursor = sqLiteDatabase.rawQuery(query, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                values.add(new ProjectModel(cursor.getString(cursor
                        .getColumnIndex("project_id")), cursor
                        .getString(cursor.getColumnIndex("project_name"))));

            } while (cursor.moveToNext());
        }
    }

    return values;
}

model class

public class ProjectModel {
private String project_id;
private String project_name;

public ProjectModel(String project_id, String project_name) {
    super();
    this.project_id = project_id;
    this.project_name = project_name;
}

public String getProject_id() {
    return project_id;
}

public String getProject_name() {
    return project_name;
}

Upvotes: 1

Shripad Jadhav
Shripad Jadhav

Reputation: 316

Steps:- 1.To store all value from single row you have to use model class

2.add following code

public ArrayList queueAllRow(){

     String query = "select * from "+DB_Table;

        Cursor cursor = ourDb.rawQuery(query, null);

        ArrayList<ModelClass> values = new ArrayList<ModelClass>();
        if(cursor.moveToFirst()){
            do {
                ModelClass ob1=new ModelClass();
                ob1.setvalue(cursor.getString(cursor.getColumnIndex("COL_NAME")));

                //set the values to other data members, same like above

                values.add(ob1);
            } while (cursor.moveToNext());

        }

        return values;      

}

Upvotes: 0

Mukesh Kumar Singh
Mukesh Kumar Singh

Reputation: 4522

You may run a raw query also to get whole row from a table in database.. you may try this code.. hope it will work for you.

   public ArrayList<Integer> queueAll_row() {

        String query = "select * from " + DB_Table;
        Cursor cursor = ourDb.rawQuery(query, null);

        ArrayList<Integer> values = new ArrayList<Integer>();
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            while (cursor.moveToNext()) {
                values.add(cursor.getInt(0));
            }
        }

        return values;
    }

Upvotes: 0

Related Questions