Android:完成尚未停用或关闭的游标

发布于 2024-11-27 02:01:58 字数 1429 浏览 2 评论 0原文

我有一个需要使用 SQLite(Android 的数据库)的应用程序。

我已经为我的模型和数据帮助程序创建了一个类似 JPA 的结构。 问题是当我在 helper.search(int id) 上调用另一个 aHelper.search(int id) 时,会记录“最终确定游标”错误。

return Child(cursor.getInt(0), new ParentHelper.search(cursor.getInt(1)));

我的类似 JPA 的模型和 datahelper 的结构如下所示。

class Parent {
   int id;
   String name
   // constructor with field-parameter
   // getters and setters
}

class Child {
   int id;
   Parent parent;
   // constructor with field-parameter
   // getters and setters
}

class ParentHelper {
   // necessary SQLiteImpelemntations
   Parent search(int id) {
      // new Cursor implementation
      cursor.moveToFirst();
      return new Parent(cursor.getInt(0), cursor.getString(1));
   }
}

class ChildHelper {

   ParentHelper parentHelper;

   void close() {
      parenHelper.close()
      SQLiteDatabase.close();
      SQLiteOpenHelper.close();
   }

   ArrayList searchAll() {
      // new Cursor implementation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      ArrayList.add(child)
      cursor.close();
   }

   // necessary SQLiteImpelemntations
   Child search(int id) {
      // new Cursor implemenation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      cursor.close();
      return child;
   }   
}

I have an application which requires the use of SQLite (Android's database).

I have created a JPA-like structure to my model and datahelper.
The problem is when I call another aHelper.search(int id) while on a helper.search(int id), the Finalizing a Cursor error logs.

return Child(cursor.getInt(0), new ParentHelper.search(cursor.getInt(1)));

The structure to my JPA-like model and datahelper goes like this.

class Parent {
   int id;
   String name
   // constructor with field-parameter
   // getters and setters
}

class Child {
   int id;
   Parent parent;
   // constructor with field-parameter
   // getters and setters
}

class ParentHelper {
   // necessary SQLiteImpelemntations
   Parent search(int id) {
      // new Cursor implementation
      cursor.moveToFirst();
      return new Parent(cursor.getInt(0), cursor.getString(1));
   }
}

class ChildHelper {

   ParentHelper parentHelper;

   void close() {
      parenHelper.close()
      SQLiteDatabase.close();
      SQLiteOpenHelper.close();
   }

   ArrayList searchAll() {
      // new Cursor implementation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      ArrayList.add(child)
      cursor.close();
   }

   // necessary SQLiteImpelemntations
   Child search(int id) {
      // new Cursor implemenation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      cursor.close();
      return child;
   }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2024-12-04 02:01:58

在返回值之前尝试关闭游标。

class ParentHelper {
// necessary SQLiteImpelemntations
Parent search(int id) {
    // new Cursor implementation
    cursor.moveToFirst();
    Parent p = new Parent(cursor.getInt(0), cursor.getString(1));
    cursor.close();
    return p;
    }
}

对您的 ChildHelper 执行相同的操作。

Try closing your Cursor before returning the value.

class ParentHelper {
// necessary SQLiteImpelemntations
Parent search(int id) {
    // new Cursor implementation
    cursor.moveToFirst();
    Parent p = new Parent(cursor.getInt(0), cursor.getString(1));
    cursor.close();
    return p;
    }
}

Do the same for your ChildHelper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文