行删除后的SQLite数据提取问题?

时间:2011-04-12 13:15:10

标签: android sqlite

朋友,

我正在尝试删除行,然后当我尝试从数据库中获取所有记录时 我得到例外任何一个人指导我,我在做什么错误?

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;
       }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

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

无论如何,在使用它之前打开db并在工作完成后关闭它是一个好习惯。