从SQLite数据库中获取价值:

时间:2015-06-12 10:04:35

标签: java android sqlite

我有一个Java程序,我在其中使用SQLite数据库。我需要获取存储在特定列和行中的值,然后将其转换为字符串。

有人可以给我一个非常简单和一般的方法吗?我想它就像"得到名字ID = 4"。

2 个答案:

答案 0 :(得分:0)

使用这种方法,我猜它会有所帮助......

public ArrayList<DbHelper> selectData() {
         try {
            Integer subCategoryArray[][] = null;    
             SQLiteDatabase db;
             db = this.getReadableDatabase(); // Read Data
                ArrayList<DbHelper> ar = new ArrayList<DbHelper>(); 
             String strSQL = "SELECT  * FROM " + TABLE_NAME;
             Cursor cursor = db.rawQuery(strSQL, null);

                if(cursor != null)
                {
                    if (cursor.moveToFirst()) {
                        subCategoryArray = new Integer[cursor.getCount()][cursor.getColumnCount()];

                        int j= 0;
                        do {                
                            subCategoryArray[j][0] = cursor.getInt(0);
                            subCategoryArray[j][1] = cursor.getInt(1);
                            subCategoryArray[j][2] = cursor.getInt(2);
                            subCategoryArray[j][3] = cursor.getInt(3);
                            subCategoryArray[j][4] = cursor.getInt(4);
                            subCategoryArray[j][5] = cursor.getInt(5);
                            subCategoryArray[j][6] = cursor.getInt(6);
                            subCategoryArray[j][7] = cursor.getInt(7);
                            subCategoryArray[j][8] = cursor.getInt(8);
                            subCategoryArray[j][9] = cursor.getInt(9);
                            subCategoryArray[j][10] = cursor.getInt(10);
                            subCategoryArray[j][11] = cursor.getInt(11);
                            subCategoryArray[j][12] = cursor.getInt(12);


                            DbHelper v= new DbHelper(subCategoryArray[j][0],subCategoryArray[j][1],subCategoryArray[j][2],subCategoryArray[j][3],subCategoryArray[j][4],subCategoryArray[j][5],subCategoryArray[j][6],subCategoryArray[j][7],subCategoryArray[j][8],subCategoryArray[j][9],subCategoryArray[j][10],subCategoryArray[j][11],subCategoryArray[j][12]);
                            ar.add(v);
                            j++;
                        } while (cursor.moveToNext());                      

                    }
                }
                cursor.close();

                return ar;

         } catch (Exception e) {
            return null;
         }

    }   

答案 1 :(得分:0)

要获得值或多个值,您需要一个光标,请参阅以下函数(返回第一个值,例如,它是完美的获取ID):

public String getSingular_Value_InTransaction(String query){
    //Declaration of variables
    Cursor a1 = null;

    try{
        a1 = database.rawQuery(query,null);
        a1.moveToFirst();

        if(a1.getString(0) != null){
            String result = a1.getString(0);
            a1.close();
            return result;
        }
        else{
            a1.close();
            return "";
        }
    }
    catch (NullPointerException ex){
        return "";
    }
    catch (CursorIndexOutOfBoundsException ex){
        return "";
    }
    catch (Exception ex){
        Log.e("-- BDD.execute_SelectCommand --","Exception", ex);
        return "";
    }
}

告诉我,如果我帮助你并做好编程!