删除行后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;
}
任何帮助将不胜感激。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在调用 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.