gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:26:54 25 4
gpt4 key购买 nike

friend ,

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

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

如有任何帮助,我们将不胜感激。

最佳答案

在调用 selectAll() 之前是否打开了数据库?你用 delete 方法关闭了它。

无论如何,最好在使用数据库之前打开数据库并在工作完成后关闭它。

关于android - 行删除后 SQLite 数据获取问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5635775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com