首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从SQLite数据库获取值:

从SQLite数据库获取值:
EN

Stack Overflow用户
提问于 2015-06-12 18:04:35
回答 2查看 1.3K关注 0票数 -6

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

有人能给我一个非常简单和通用的方法吗?我把它想象成类似于"get Name where ID=4“的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-12 18:09:43

要获取值或多个值,您需要一个游标,请参见以下函数(例如,该函数返回最适合获取id的第一个值):

代码语言:javascript
复制
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 "";
    }
}

告诉我我是不是帮了你,还有好的编程!

票数 0
EN

Stack Overflow用户

发布于 2015-06-12 18:07:15

使用这个方法,我想它会很有帮助。

代码语言:javascript
复制
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;
         }

    }   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30800422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档