SQLite unable to open database file (code 14) on frequent “SELECT” query

后端 未结 5 783
無奈伤痛
無奈伤痛 2020-12-16 04:09

I have following class \"Singleton\" to handle SQLite connection and to make sure to have 1 instance of connection for whole process/app:

public class DBCon         


        
5条回答
  •  眼角桃花
    2020-12-16 04:37

    In your Application class

    public static SQLiteDatabase database;
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        SQLiteOpenHelper helper = Database.getInstance(getApplicationContext());
    
        if (database == null) {
            database = helper.getWritableDatabase();
        } else {
            if (!database.isOpen()) {
                database = helper.getWritableDatabase();
            }
        }
    }
    

    In your queries class

     public DataSources() {
        database = ApplicationController.database;
     }
    

    That way you create a writeable db object once

提交回复
热议问题