首页 > 解决方案 > SQLiteConnection,数据库被泄露,快速触发更新后

问题描述

我的错误日志:

    2019-11-28 11:46:59.415 13936-13936/com.example.thongapps E/SQLiteLog: (14) cannot open file at line 35803 of [605907e73a]
2019-11-28 11:46:59.415 13936-13936/com.example.thongapps E/SQLiteLog: (14) os_unix.c:35803: (24) open(/data/user/0/com.example.thongapps/databases/dbThong) - 
2019-11-28 11:46:59.421 13936-13936/com.example.thongapps E/SQLiteDatabase: Failed to open database '/data/user/0/com.example.thongapps/databases/dbThong'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:724)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:295)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
        at com.example.thongapps.DatabaseHelper.<init>(DatabaseHelper.java:86)
        at com.example.thongapps.CustomeAdapter.removeDBItem(CustomeAdapter.java:448)
        at com.example.thongapps.CustomeAdapter.access$100(CustomeAdapter.java:23)
        at com.example.thongapps.CustomeAdapter$1.afterTextChanged(CustomeAdapter.java:114)
        at android.widget.TextView.sendAfterTextChanged(TextView.java:9429)
        at android.widget.TextView.setText(TextView.java:5434)
        at android.widget.TextView.setText(TextView.java:5281)
        at android.widget.EditText.setText(EditText.java:117)
        at android.widget.TextView.setText(TextView.java:5238)
        at com.example.thongapps.CustomeAdapter.getView(CustomeAdapter.java:84)

奇怪的是,当我以缓慢的方式进行操作时,一切都很好,但是当我以更快的方式来编辑文本时,数据库泄漏并且无法打开。我的触发器功能是当用户在editText中设置0/删除任何内容(listview内的editText,行中的多个editText和超过1行)时删除,然后在数据库中删除记录。当我没有添加任何问题(使用按钮)时,但由于delete某些原因,我需要手动更新行中的每个更新。在某些情况下,当记录尚未保存并且用户更改为 0/删除 editText 中的数据时,但这已经在我的例外中。

所以我的问题是为什么当我在其他函数中执行 CRUD 数据时很好,但在函数中删除 editText 卡住了。只有当我快速删除/设置为 0(触发删除记录)时,但当我以慢速执行时,一切都很好。

只是不太清楚我的问题几乎相同: 在 android 中执行 getwritabledatabase() 时出现错误

那么我怎样才能初始化数据库呢?我的数据库有问题,

    private Context mContext;
    SQLiteDatabase db;        

    public DatabaseHelper(Context context ){
        super(context, DATABASE_NAME, null,DATABASE_VERSION );
        mContext = context;
        db = this.getWritableDatabase();
    }

    public static DatabaseHelper getInstance(Context ctx) {
        if (dInstance == null) {
            dInstance = new DatabaseHelper(ctx.getApplicationContext());
        }
        return dInstance;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        Log.w(DatabaseHelper.class.getName(),
                "Upgrading database from version " + i + " to "
                        + i1 + ", which will destroy all old data");

        db.execSQL(tableDBVersion.UPGRADE_TABLE_DBVERSION);
        onCreate(db);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(tableDBVersion.CREATE_TABLE_DBVERSION);
        db.execSQL(tableDBVersion.SEED_TABLE_DBVERSION);
    }

从资产中复制 sqlite 数据库时使用“无法打开数据库”错误在 Android 4.2中,仍然出现相同的错误。如果它正确地实现我的代码的方法:

public DatabaseHelper(Context context ){
    super(context, DATABASE_NAME, null,DATABASE_VERSION );
    mContext = context;
    if(!checkDataBase()){
        db = this.getWritableDatabase();
    }
}

private boolean checkDataBase() {
    File databasePath = mContext.getDatabasePath(DATABASE_NAME);
    return databasePath.exists();
}

标签: androidandroid-edittextandroid-database

解决方案


推荐阅读