API Database Error 16+

0

Well folks, I'm working on my application, and everything was perfectly fine ... I finished it with your help, but I found another problem. I was testing it on Android 2.3.3 as it was more popular and it opened faster on my computer, but when I went to test it on my tablet (which is API16) and the emulator with Android 4.1.2 (API16) it is showing a weird mistake. When I squeeze it in 2.3 it opens up perfectly, retrieving DB data and everything ... However, when I try to run it on a newer API, it's past that my Database does not exist (?!). Could anyone tell me if there has been any change in the way I work with internal DBs in recent APIs? I was told that some of my commands might have fallen out of use, but the problem is pointing only to SQL, not to Android.

    
asked by anonymous 25.03.2014 / 16:50

1 answer

1

Stephano, could you include the error logs you got?

You may be experiencing the same problem that I had when I tested my app in versions 4. +, as I developed it based on version 2.3 as well.

The problem is related to how you close the database resources. I discovered, after a bit of research, that the correct way to close resources is as follows:

Example:

public class DatabaseHandler extends SQLiteOpenHelper{
// ...
}
SQLiteOpenHelper dbHandler = new DatabaseHandler(context);

// método para escrita
SQLiteDatabase db = null;
try {
    db = dbHandler.getWritableDatabase();
    // ... executa operaçao de escrita
} finally {
    if (db != null && db.isOpen()) {
        db.close();
    }
}

// método para leitura
SQLiteDatabase db = null;
Cursor cursor = null;
try {
    db = dbHandler.getReadableDatabase();
    // ... executa operaçao de leitura com o cursor

} finally {
    if (cursor != null) {
        cursor.close();
    }
}

Note: For reading, you should not close the instance of SQLiteDatabase and you should not close SQLiteOpenHelper with each write / read.

If your problem is the same as I had, this will solve for all versions of android > = 2.3. I can not tell you that your problem is this because I do not have the logs.

I hope I have helped. :)

    
26.03.2014 / 15:07