HttpReader OnResultReadyListener: attempt to re-open an already-closed object: SQLiteDatabase

61 views Asked by At

I'm trying to convert a csv file that I get online and try to call a execSQL(String) based on the results.

The only problem is dat I send my SQLiteDatabase with my function in the parameter, but that way I can't acces it from within my resultReady(String result){} if I don't make the parameter final.

But when I make the SQLiteDatabase parameter final, I get the error message: "attempt to re-open an already-closed object: SQLiteDatabase: ..."

Does anyone know how I could fix this?

The function:

private void insertTalen(SQLiteDatabase db) {
    // lijst van alle talen met taalcode opgehaald van het internet
    HttpReader httpReader = new HttpReader();
    httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
        @Override
        public void resultReady(String result) {
            CsvHelper csvHelper = new CsvHelper();
            List<Taal> talen = csvHelper.getTalenFromCsv(result);
            for (Taal taal: talen) {
                String SQLScript = "INSERT INTO taal (engelseTaalNaam, taalCode) VALUES ('";
                SQLScript += taal.getEngelseNaam();
                SQLScript += "', '";
                SQLScript += taal.getTaalCode();
                SQLScript += "');";
                db.execSQL(SQLScript);
            }
        }

    });

    httpReader.execute("https://raw.githubusercontent.com/datasets/language-codes/master/data/language-codes.csv");
}

And this function is called from my oncreate:

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE_TAAL = "CREATE TABLE taal (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "engelseNaam TEXT," +
            "taalCode TEXT)";
    db.execSQL(CREATE_TABLE_TAAL);

    String CREATE_TABLE_GEBRUIKER = "CREATE TABLE gebruiker (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "highscoreGebruikerId INTEGER," +
            "eigenTaalId INTEGER," +
            "vreemdeTaalId INTEGER," +
            "FOREIGN KEY (eigenTaalId) REFERENCES taal(id)," +
            "FOREIGN KEY (vreemdeTaalId) REFERENCES taal(id))";
    db.execSQL(CREATE_TABLE_GEBRUIKER);

    String CREATE_TABLE_OPGESLAGEN_WOORD = "CREATE TABLE opgeslagenWoord (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "taalId INTEGER," +
            "naam TEXT," +
            "engelseVersieId INTEGER," +
            "FOREIGN KEY (taalId) REFERENCES taal(id)," +
            "FOREIGN KEY (engelseVersieId) REFERENCES opgeslagenWoord(id))";
    db.execSQL(CREATE_TABLE_OPGESLAGEN_WOORD);

    String CREATE_TABLE_GEKEND_WOORD = "CREATE TABLE gekendWoord (" +
            "opgeslagenWoordId INTEGER PRIMARY KEY ," +
            "datumverloopt TEXT," +
            "niveauGekend INTEGER," +
            "FOREIGN KEY (opgeslagenWoordId) REFERENCES opgeslagenWoord(id))";
    db.execSQL(CREATE_TABLE_GEKEND_WOORD);

    this.db = db;

    insertTalen(db);
    insertGebruiker(db);
}
0

There are 0 answers