zhengdanimu 2013-01-24 05:35 采纳率: 88.9%
浏览 3435
已采纳

在android中从sqlite中获得数据很慢

我的代码是:

DatabaseHandler db = new DatabaseHandler(this);
System.out.println("Start - " + System.currentTimeMillis());
for (int i = 1; i < db.getChampsCount() + 1; i++) {
    String name = db.getChampInfo(i, "name");
    String title = db.getChampInfo(i, "title");
    String thumb = db.getChampInfo(i, "thumb");
    System.out.println("End - " + System.currentTimeMillis());
    [...]
}

和这个

String getChampInfo(int id, String col) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT " + col + " FROM champions WHERE id = " + id, new String[] {});
    if (cursor != null)
        cursor.moveToFirst();

    db.close();
    return cursor.getString(0);
}

这是DatabaseHelper类的一部分

这个运行没有问题,但是问题是它执行起来时间很长(在我的android机子上是2089ms)。这些字符串是UI的一部分,所以我不认为我能把他放到另外一个线程中。如果要让这段代码跑的快一点,我应该怎么做?

确切的说是110行

  • 写回答

2条回答

  • Sueyexin 2013-01-29 06:52
    关注

    不应该是用单独的语句,你不应该用单独的sql语句?

    只是创建一个ArrayList,在这个activity类中存储所有需要的数值。
    例如: ArrayList myData;

    现在在数据库类的帮助下写一个function,就像下边:
    //得到你想要的数据

    public ArrayList<String> getAllData() {          
        ArrayList<String> subTitleList = null;         
        Cursor cursor = null;          
        try {              
        String queryString = "SELECT * FROM champions";              
            cursor =  db.rawQuery(queryString, null);              
            if (cursor != null && cursor.moveToFirst()) {                 
                subTitleList = new ArrayList<String>();                 
                do {                     
                    String nextUser = new String(cursor.getString(cursor.getColumnIndex("name")));                     
                    String nextUser = new String(cursor.getString(cursor.getColumnIndex("title")));                     
                    String nextUser = new String(cursor.getString(cursor.getColumnIndex("thumb")));                     
    
                    subTitleList.add(nextUser);                 
                } 
                while (cursor.moveToNext());   
                System.out.println("it comes in SubTitleList");
            }         
        } 
        catch (Exception e) {             
            e.printStackTrace();             
            subTitleList = null;         
        } 
        finally {             
            if (cursor != null && !cursor.isClosed()) {                 
                cursor.deactivate();                 
                cursor.close();                 
                cursor = null;             
            }             
            if(db != null){                 
                db.close();             
            }         
        }
        //System.out.println("SubTitleList is: "+subTitleList);
        return subTitleList;   
    }
    

    现在在你的activity类中你可以调用这个方法,然后从myData ArrayList获得所有你需要的数据。

    myData = db.getAllData(); // 我想如果你想要抓所有的数据的话是不需要ID的。
    

    希望你能明白

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?