我正在开发的有超过4K行查询,在一段时间内,数据库的应用程序,并且每行有90个领域(字符串)。现在的问题是,如果我从数据库中选择*,我的光标变得非常大(超过4MB)。光标在Android的限制为1MB。 我怎样才能解决这个问题,或者什么是最优雅的方式来解决这个? 它可以在较小的块拆分数据库和查询出来?
I'm working to develop an application that has to query at some time, a database with over 4k rows, and each row has 90 fields (Strings). The problem is that if I select * from database, my cursor gets really big (over 4MB). And the cursor in android is limited to 1MB. How can I solve this, or what's the most elegant method to workaround this? It is possible to split database in smaller chunks and query them out?
谢谢, Arkde
推荐答案
我找到了一种方法来处理这个问题,我想所有谁需要它来共享。
I found a way to handle this and I want to share with all who need it.
int limit = 0;
while (limit + 100 < numberOfRows) {
//Compose the statement
String statement = "SELECT * FROM Table ORDER someField LIMIT '"+ limit+"', 100";
//Execute the query
Cursor cursor = myDataBase.rawQuery(statement, null);
while (cursor.moveToNext()) {
Product product = new Product();
product.setAllValuesFromCursor(cursor);
productsArrayList.add(product);
}
cursor.close();
limit += 100;
}
//Compose the statement
String statement = "SELECT * FROM Table ORDER someField LIMIT '"+ (numberOfRows - limit)+"', 100";
//Execute the query
Cursor cursor = myDataBase.rawQuery(statement, null);
while (cursor.moveToNext()) {
Product product = new Product();
product.setAllValuesFromCursor(cursor);
productsArrayList.add(product);
}
cursor.close();
主要思路是分割你的数据,这样你就可以使用光标,它应该被使用。它在2秒工作5K行,如果你有索引的表。
The main idea is to split your data, so you can use the cursor as it should be used. It's working under 2 s for 5k rows if you have indexed table.
谢谢, Arkde
相关推荐
最新文章