Search code examples
androidsqlitesqliteopenhelper

Trouble with SqLite Journal files. "file is encrypted or is not a database"


I still have a lot of trouble handling multiple SQLite DB-files. Every DB-file has its own journal-file, which I assumed should disappear, once the writing is done. In fact they stay, so that I'm in trouble deleting the DB-file, because the journal-files seem to block the delete request (file delete).

I'm running out of ideas now and to be honest, I'm not very experienced with SqLite, so maybe there is a simple error somewhere else.

I'm using SQLiteOpenHelper. Here is my onCreate().

@Override
public void onCreate(SQLiteDatabase db) {

    Log.d(TAG, "onCreate");

    // SQL statement to create track table
    String CREATE_TRACK_TABLE = "CREATE TABLE track ( "
            + "name TEXT PRIMARY KEY, " + "version REAL, "
            + "creatingDate TEXT )";

    // create track table
    db.execSQL(CREATE_TRACK_TABLE);

    ...
}

Here is an example how I'm writing to DB.

// add single track
public void addTrack(Track track) {

    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(Track.KEY_NAME, track.getName()); // get name
    values.put(Track.KEY_VERSION, track.getVersion()); // get version
    values.put(Track.KEY_CREATINGDATE, track.getCreatingDateString()); // get
                                                                        // version

    // 3. insert
    db.insert(Track.TABLE_NAME, // table
            null,               // nullColumnHack
            values);            // key/value -> keys = column names/ values = column
                                // values

    // 4. close
    db.close();
}

Here is an example how I'm reading from DB.

// Get single track
public Track getTrack(String name) {

    // 1. get reference to readable DB
    SQLiteDatabase db = this.getReadableDatabase();

    // 2. build query
    Cursor cursor = db.query(Track.TABLE_NAME, // a. table
            Track.COLUMNS, // b. column names
            " name = ?", // c. selections
            new String[] { name }, // d. selections args
            null, // e. group by
            null, // f. having
            null, // g. order by
            null); // h. limit

    // 3. if we got results get the first one
    if (cursor != null)
        cursor.moveToFirst();

    // 4. build track object
    Track track = new Track();
    track.setName(cursor.getString(0));
    track.setVersion(Float.parseFloat(cursor.getString(1)));

    if (cursor != null)
        cursor.close();

    // 5. return track
    return track;
}

When I'm done recording, I still have a -journal file. Does anybody know if the journal-file has to be present or should disappear? Do I have to close the SQLiteOpenHelper in any specific manner? The journal files are ruining my list of Databases, because every record has a ghost-entry, caused by the journal-file. Besides, I receive this error, when I'm trying to load an existing DB-File:

E/SQLiteLog﹕ (26) file is encrypted or is not a database
E/DefaultDatabaseErrorHandler﹕ Corruption reported by sqlite on database:    /data/data/etw.com.roadtracker/databases/TestTrack-journal
E/DefaultDatabaseErrorHandler﹕ deleting the database file: /data/data/etw.com.roadtracker/databases/TestTrack-journal

If you need more information, please ask for it, I'm desperate... :-( Thanks in advance!


Solution

  • Just to close this one, a that time I was trying to save different entities into their own databases. Once I wanted to delete an entity, I tried to delete the db file. Now I'm pretty sure it's bad practice, the better way is to store entities in a single db and manage them with CRUD(Create, Read, Update, Delete)-methods.