API 16+ Database Error

Asked

Viewed 81 times

0

Well folks, I’m working on my application, and everything was perfectly fine... I had finished it with your help, but I found another problem. I was testing it on Android 2.3.3 because it was more popular and opened faster on my computer, but when I went to test it on my tablet (which is API16) and on the emulator with Android 4.1.2(API16) it is presenting a strange error. When I run it in 2.3 it opens perfectly, retrieving data from the BD and everything... However, when I try to run it in a newer API, it is passed to me that my database does not exist(?!). Would anyone know if there’s been any change in the way you work with internal comics in recent Apis? I was told that some of my commands could have fallen into disuse, but the problem is pointing only to SQL, and not to Android.

1 answer

1


Stéfano, you could include the error logs you got?

Maybe you are facing the same problem I had when I tested my app in versions 4.+, because 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 little research, that the correct way to close the 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 the Sqliteopenhelper every writing/reading.

If your problem is the same as I had, it will solve for all versions of android >= 2.3. I can’t tell you that’s your problem because I don’t have the logs.

I hope I’ve helped. :)

  • That’s exactly what gave error! I looked here and managed to fix, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.