如何在Sqlite中检索整行数据

时间:2014-10-22 05:37:25

标签: android android-sqlite crud

我想在sqlite数据库中一次检索整行。所有这些都是字符串。我写了一个方法。 如果有人可以检查这个错误。 谢谢

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

3 个答案:

答案 0 :(得分:0)

您也可以运行原始查询以从数据库中的表中获取整行。 你可以尝试这段代码..希望它对你有用。

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

答案 1 :(得分:0)

步骤: - 1.要存储单行的所有值,必须使用模型类

2.添加以下代码

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;      

}

答案 2 :(得分:0)

请试试这个,希望对你有所帮助。

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