【问题标题】:Get value from SQLite database:从 SQLite 数据库中获取值:
【发布时间】:2015-08-28 07:22:40
【问题描述】:

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

有人可以给我一个非常简单和通用的方法吗?我想它类似于“get Name where ID=4”。

【问题讨论】:

  • 网络上有数百个教程。
  • 你不应该要求一个现成的代码,而应该为教程做。
  • something like "get Name where ID=4" 几乎"SELECT Name FROM Names WHERE id = 4"。 Names 是(显然)包含您的姓名的表的名称。

标签: java android sqlite


【解决方案1】:

要获取一个值或多个值,您需要一个游标,请参见以下函数(例如,返回第一个值非常适合获取 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 "";
    }
}

告诉我我是否帮助过你和好的编程!

【讨论】:

    【解决方案2】:

    用这个方法,我想会有帮助的……

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      相关资源
      最近更新 更多