Java 从SQLite数据库获取值:

Java 从SQLite数据库获取值:,java,android,sqlite,Java,Android,Sqlite,我有一个使用SQLite数据库的Java程序。我需要获取存储在特定列和行中的值,然后将其转换为字符串 有人能给我一个非常简单和一般的方法来做这件事吗?我认为它类似于get Name where ID=4。使用此方法,我想它会有所帮助 public ArrayList<DbHelper> selectData() { try { Integer subCategoryArray[][] = null; SQLi

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


有人能给我一个非常简单和一般的方法来做这件事吗?我认为它类似于get Name where ID=4。

使用此方法,我想它会有所帮助

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

    }   

要获取值或多个值,您需要一个光标,请参阅以下函数,该函数返回第一个值。例如,获取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 "";
    }
}

告诉我我是否帮助过你和优秀的编程

web上有数百个教程。您不应该要求提供现成的代码,但应该为教程提供。类似于get Name where ID=4:从Name where ID=4中选择Name。名称显然是包含您的名称的表的名称。