删除行后SQLite数据获取问题?

发布于 2024-10-31 12:35:47 字数 1317 浏览 0 评论 0原文

朋友,

我正在尝试删除行,然后当我尝试从数据库中获取所有记录时 我遇到异常,有人指导我我犯了什么错误吗?

private static final String DATABASE_NAME = "example.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "table1";

   private Context context;
   private SQLiteDatabase db;

 public dbHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
   }


    public List<String> selectAll() {
          List<String> list = new ArrayList<String>();
          Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
            null, null, null, null, "name desc");
          if (cursor.moveToFirst()) {
             do {
                list.add(cursor.getString(0));
             } while (cursor.moveToNext());
          }
          if (cursor != null && !cursor.isClosed()) {
             cursor.close();
          }
          return list;
       }

       public int DeleteName(String name)
       {
           int rowsEffected = this.db.delete(TABLE_NAME,"name=?", new String[] {name});
           this.db.close();
           return rowsEffected;
       }

任何帮助将不胜感激。

friends,

i am trying to delete row and then when i try to fetch all records from database
i get exception any one guide me what mistake am i doing?

private static final String DATABASE_NAME = "example.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "table1";

   private Context context;
   private SQLiteDatabase db;

 public dbHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
   }


    public List<String> selectAll() {
          List<String> list = new ArrayList<String>();
          Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
            null, null, null, null, "name desc");
          if (cursor.moveToFirst()) {
             do {
                list.add(cursor.getString(0));
             } while (cursor.moveToNext());
          }
          if (cursor != null && !cursor.isClosed()) {
             cursor.close();
          }
          return list;
       }

       public int DeleteName(String name)
       {
           int rowsEffected = this.db.delete(TABLE_NAME,"name=?", new String[] {name});
           this.db.close();
           return rowsEffected;
       }

any help would be appreciated.

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

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

发布评论

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

评论(1

风轻花落早 2024-11-07 12:35:48

你在调用 selectAll() 之前打开过 db 吗?
您在删除方法中关闭了它。

无论如何,在使用数据库之前打开数据库并在工作完成后关闭它是一个很好的做法。

Have you opened db before call selectAll()?
You closed it in delete method.

Anyway it is good practice to open db before work with it and close it after the work is done.

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