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条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?