正确关闭我的数据库 [英] closing my database correctly

查看:102
本文介绍了正确关闭我的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我关闭它(支票code以下),我无法创建我的数据库。如果我不关闭它的一切工作正常,但我在logcat中获得几个错误说的close()从来没有明确要求。如果我已经创建了数据库,然后添加的close()这些错误会消失,但我不能重新安装我的应用程序,并与运行它。

I cannot create my database when I close it (check code below). If I do not close it everything works fine but I get several errors in logcat saying that close() was never explicitly called. If I have created the database and then add the close() those errors will go away, but I cannot reinstall my app and run it with that.

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_score, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " + KEY_SCORE1
                + " INTEGER TEXT NOT NULL, " + KEY_SCORE2
                + " INTEGER TEXT NOT NULL);");
        db.close(); <---problem
    }

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

public Database(Context c) {
    ourContext = c;
}

public Database open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public long createEntry(String score, String score2) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_SCORE1, score);
    cv.put(KEY_SCORE2, score2);

    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

所以,问题是:如何做我称之为 db.close(),并有我的应用程序运行在正确

So the question is: how do I call db.close() and have my app run on correctly?

推荐答案

您不必调用 db.close(); 你的的onCreate()在DbHelper类。

You don't have to call db.close(); inside your onCreate() in your DbHelper class.

相反,你必须使用从DbHelper类获得一个数据库实例
DbHelper_instance.getReadableDatabase() DbHelper_instance.getWritableDatabase()并赋值给的 SQLiteDatabase 对象(让我们把它定义为SQLiteDatabase数据库)。

Instead you have to acquire a database instance from your DbHelper class using DbHelper_instance.getReadableDatabase() or DbHelper_instance.getWritableDatabase() and assign that to SQLiteDatabase object (let's define it as SQLiteDatabase database).

完成后使用的数据库,在您关闭游标,你可以这样做:

After you finish using the database, and you close your cursor, you can do something like:

        if (database.isOpen()) {
            Log.d(TAG + " Closing SQLite Database connection...");
            database.close();
        }

您可以把在finally块,以确保它会一直执行(记住,你的数据库操作中的try / catch包围拍摄)。

You can put that in finally block, to be sure it will always execute (taking in mind that your DB operations are surrounded in try/catch).

这篇关于正确关闭我的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆