从android上的sqlite获取数据真的很慢

时间:2013-01-24 03:54:33

标签: android sql performance 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 个答案:

答案 0 :(得分:4)

您应该不使用Single sql语句而不是单个语句吗?

只需创建一个ArrayList,它将在Activity类中存储所有必需的值。

例如:ArrayList<String> myData;

现在在数据库助手类中创建一个如下函数:

 // TO get All Data of datanase which you want
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;   
}

现在在您的活动类中,您可以调用此函数并从myData ArrayList获取所有必需的数据。

myData = db.getAllData(); // i think there is no need of any ID if you are fetching all the data.

希望你明白我的意思。

答案 1 :(得分:1)

你绝对可以在AsyncTask中运行它们。您所要做的就是将数据传递给任务的参数,或者如果您无法解决这个问题,只需让您的任务有一个构造函数来获取参数并将其调用如下:

MyAsync ma = new MyAsync(stuff, stuff, stuff);
ma.execute();

在任务的onPostExecute()中,您可以从后台运行的查询中获取数据,然后您可以更新UI。

其他人也是对的。如果你可以将你最好的查询组合起来,但是这样的表不会给你带来很大的性能提升,至少我不认为。