gpt4 book ai didi

Android SQLite 问题 "Cannot perform this operation because the connection pool has been closed"

转载 作者:行者123 更新时间:2023-11-30 01:46:31 29 4
gpt4 key购买 nike

我有时遇到 SQLite 问题,有时我的应用程序崩溃,错误是

 Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.

这是我的函数,错误来自这里

public List<contacts> getAllcontacts() {
List<contacts> contactsl = new LinkedList<contacts>();

// 1. build the query
String query = "SELECT * FROM contacts";

// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);

// 3. go over each row, build a contact and add it to list
contacts contact = null;
if (cursor.moveToFirst()) {
do {
contact = new contacts();

contact.setName(cursor.getString(1));
contact.setNumero(cursor.getString(3));
contact.setProfil(cursor.getString(2));
contact.setShow(cursor.getString(5));
contact.setBlocked(cursor.getString(4));
contact.setObjectid(cursor.getString(6));
contactsl.add(contact);
} while (cursor.moveToNext());
}

return contactsl;
}

当我测试我的应用程序并尝试多次启动该功能时,当我执行该功能时显示错误。

这是数据库的创建:

private static sql sInstance;

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "Contact";

public static sql getInstance(Context context) {

// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
if (sInstance == null) {
sInstance = new sql(context.getApplicationContext());
}
return sInstance;
}

public sql(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String CREATETABLE = "CREATE TABLE contacts ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, "+
"profil TEXT, "+
"phone TEXT UNIQUE ,"+
"blocked TEXT, "+
"show TEXT , "+
"objectid TEXT )";

db.execSQL(CREATETABLE);
}




@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int `newVersion) {`
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");

this.onCreate(db);
}

最佳答案

如果您过早关闭 DBHelper,您可能会收到此错误。如果您关闭 Helper 并关闭它,请搜索您的代码,看看您是否仍然遇到该错误。

否则可以尝试使用此模板来实例化您的 DBHelper。

public class MyDBHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "myDatabase.db";
private static final int SCHEMA = 1;
private static volatile MyDBHelper sInstance;


private MyDBHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA);
}

public static MyDBHelper getInstance(Context context) {
if (sInstance == null) {
synchronized (MyDBHelper.class) {
if (sInstance == null) {
sInstance = new MyDBHelper(context.getApplicationContext());
}
}
}
return sInstance;
}

@Override
public void onCreate(SQLiteDatabase db) {
// create your tables here
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// handle db upgrade or leave blank
}
}

作为对您评论的回答,您仍然可以在实例完成后关闭数据库

@Override
public void finalize() throws Throwable {
sInstance.close();
super.finalize();
}

关于Android SQLite 问题 "Cannot perform this operation because the connection pool has been closed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640166/

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