android– 尝试重新打开已经关闭的对象sqlitedatabase

android– 尝试重新打开已经关闭的对象sqlitedatabase,第1张

概述我有一个databaseHandler.我需要计算表中的行数.应用程序因此错误而崩溃:android尝试重新打开已经关闭对象sqlitedatabase.我只是在活动中使用此代码:db.getContactsCount();但应用程序崩溃了.另外我想重置表(删除表行).我添加了以下方法:publicvoiddeleteTable(){

我有一个databaseHandler.我需要计算表中的行数.应用程序因此错误而崩溃:android尝试重新打开已经关闭的对象sqlitedatabase.
我只是在活动中使用此代码:

db.getContactsCount();

但应用程序崩溃了.另外我想重置表(删除表行).我添加了以下方法:

public voID deletetable() {        sqliteDatabase db = this.getWritableDatabase();        db.delete("contacts", null, null);    }

它运作良好,但我不能使用这个:

@OverrIDepublic voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {    // Drop older table if existed    db.execsql("DROP table IF EXISTS " + table_CONTACTS);    // Create tables again    onCreate(db);}

这是databasehandler:

public class DatabaseHandler extends sqliteOpenHelper {// All Static variables// Database Versionprivate static final int DATABASE_VERSION = 1;// Database nameprivate static final String DATABASE_name = "contactsManager";// Contacts table nameprivate static final String table_CONTACTS = "contacts";// Contacts table Columns namesprivate static final String KEY_ID = "ID";private static final String KEY_name = "name";private static final String KEY_PH_NO = "phone_number";public DatabaseHandler(Context context) {    super(context, DATABASE_name, null, DATABASE_VERSION);}// Creating tables@OverrIDepublic voID onCreate(sqliteDatabase db) {    String CREATE_CONTACTS_table = "CREATE table " + table_CONTACTS + "("            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_name + " TEXT,"            + KEY_PH_NO + " TEXT" + ")";    db.execsql(CREATE_CONTACTS_table);}// Upgrading database@OverrIDepublic voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {    // Drop older table if existed    db.execsql("DROP table IF EXISTS " + table_CONTACTS);    // Create tables again    onCreate(db);}/** * All CRUD(Create, Read, Update, Delete) Operations */// Adding new contactvoID addContact(Contact contact) {    sqliteDatabase db = this.getWritableDatabase();    ContentValues values = new ContentValues();    values.put(KEY_name, contact.getname()); // Contact name    values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone    // Inserting Row    db.insert(table_CONTACTS, null, values);    db.close(); // Closing database connection}// Getting single contactContact getContact(int ID) {    sqliteDatabase db = this.getReadableDatabase();    Cursor cursor = db.query(table_CONTACTS, new String[] { KEY_ID,            KEY_name, KEY_PH_NO }, KEY_ID + "=?",            new String[] { String.valueOf(ID) }, null, null, null, null);    if (cursor != null)        cursor.movetoFirst();    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),            cursor.getString(1), cursor.getString(2));    // return contact    return contact;}// Getting All Contactspublic List<Contact> getAllContacts() {    List<Contact> contactList = new ArrayList<Contact>();    // Select All query    String selectquery = "SELECT  * FROM " + table_CONTACTS;    sqliteDatabase db = this.getWritableDatabase();    Cursor cursor = db.rawquery(selectquery, null);    // looPing through all rows and adding to List    if (cursor.movetoFirst()) {        do {            Contact contact = new Contact();            contact.setID(Integer.parseInt(cursor.getString(0)));            contact.setname(cursor.getString(1));            contact.setPhoneNumber(cursor.getString(2));            // Adding contact to List            contactList.add(contact);        } while (cursor.movetoNext());    }    // return contact List    return contactList;}// Updating single contactpublic int updateContact(Contact contact) {    sqliteDatabase db = this.getWritableDatabase();    ContentValues values = new ContentValues();    values.put(KEY_name, contact.getname());    values.put(KEY_PH_NO, contact.getPhoneNumber());    // updating row    return db.update(table_CONTACTS, values, KEY_ID + " = ?",            new String[] { String.valueOf(contact.getID()) });}// Deleting single contactpublic voID deleteContact(Contact contact) {    sqliteDatabase db = this.getWritableDatabase();    db.delete(table_CONTACTS, KEY_ID + " = ?",            new String[] { String.valueOf(contact.getID()) });    db.close();}// Getting contacts Countpublic int getContactsCount() {    String countquery = "SELECT  * FROM " + table_CONTACTS;    sqliteDatabase db = this.getReadableDatabase();    Cursor cursor = db.rawquery(countquery, null);    cursor.close();    // return count    return cursor.getCount();}public voID Upgrade (sqliteDatabase db, int oldVersion, int newVersion) {    String CREATE_CONTACTS_table = "CREATE table " + table_CONTACTS + "("            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_name + " TEXT,"            + KEY_PH_NO + " TEXT" + ")";    db.execsql(CREATE_CONTACTS_table);}// Deleting single contact    public voID deletetable() {        sqliteDatabase db = this.getWritableDatabase();        db.delete("contacts", null, null);    }}

解决方法:

它发生的原因是:

db.close();

在方法中:

voID addContact(联系联系人)

public voID deleteContact(联系方式)

您不应该关闭与底层数据库的连接,除非您真的不打算再使用它.
使用sqliteOpenHelper:关闭,当你完成你的工作.

此外,对getReadableDatabase()和getWriteableDatabase()的调用99%一次返回相同的数据库对象,并且它们不会重新初始化您手动关闭的数据库连接.

不要被这些方法名称所迷惑.

总结

以上是内存溢出为你收集整理的android – 尝试重新打开已经关闭的对象sqlitedatabase全部内容,希望文章能够帮你解决android – 尝试重新打开已经关闭的对象sqlitedatabase所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存