机器人如何在Android中查询庞大的数据库(光标大小限制为1MB) [英] Android how to query huge database in android (cursor size is limited to 1MB)

查看:187
本文介绍了机器人如何在Android中查询庞大的数据库(光标大小限制为1MB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的有超过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

这篇关于机器人如何在Android中查询庞大的数据库(光标大小限制为1MB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆