Problem with SQLiteOpenHelper on Android 2.X and 3.X

So, I made a big mistake in testing my code on Android 4.0 + and thought that it would work fine on other versions. But I ran into problems with 2.X and 3.X using SQLiteOpenHelper.

First code:

public class HelperDB extends SQLiteOpenHelper implements BaseColumns { public static final int DATABASE_VERSION = 2; public static final String DATABASE_NAME = "MYDB.db"; public static final String TABLE_NAME = "MYtable"; public static final String TABLE_NAME_LOGO = "MYLogos"; public static final String COLUMN_NAME_1 = "xxxxx"; public static final String COLUMN_NAME_2 = "yyyy"; public static final String COLUMN_NAME_3 = "zzzz"; public static final String COLUMN_NAME_4 = "aaaa"; public static final String COLUMN_NAME_LOGO = "logo"; public static final String COLUMN_NAME_5 = "3333"; public static final String COLUMN_NAME_6 = "fffff"; public static final String COLUMN_NAME_7= "Abc"; public HelperDB(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db){ db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY autoincrement," + COLUMN_NAME_1 + " TEXT not null," + COLUMN_NAME_2 + " TEXT not null," + COLUMN_NAME_3 + " TEXT not null," + COLUMN_NAME_4 + " LONG not null," + COLUMN_NAME_5 + " INT DEFAULT 99," + COLUMN_NAME_6 +" INT DEFAULT 99);"); db.execSQL("CREATE TABLE " + TABLE_NAME_LOGO + "(" + _ID + " INTEGER PRIMARY KEY autoincrement," + COLUMN_NAME_6 + " TEXT not null," + COLUMN_NAME_7 + " INTERGET not null);"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } ****************************************************************************** public class MatchesDB { private HelperDB dbHelper; private SQLiteDatabase dbw, dbr; public static int id; public MatchesDB(Context context){ dbHelper = new HelperDB(context); dbw = dbHelper.getWritableDatabase(); dbr = dbHelper.getReadableDatabase(); } public void fillinfotable(){} And all other functions to query the database and delete etc 

Creating a database by calling

  MatchesDB newdb = new MatchesDB(context); newdb.fillinfotable(); 

All this works fine on all 4.0+ devices. However, when I try to run on 2.x devices, I get the following error:

 07-14 21:29:14.890: E/Database(22231): CREATE TABLE android_metadata failed 07-14 21:29:14.898: E/Database(22231): CREATE TABLE android_metadata failed 07-14 21:29:15.640: E/Database(22231): Failed to setLocale() when constructing, closing the database 07-14 21:29:15.640: E/Database(22231): android.database.sqlite.SQLiteException: database is locked 07-14 21:29:15.640: E/Database(22231): at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method) 07-14 21:29:15.640: E/Database(22231): at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2000) 07-14 21:29:15.640: E/Database(22231): at android.database.sqlite.SQLiteDatabase. <init>(SQLiteDatabase.java:1857) 07-14 21:29:15.640: E/Database(22231): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:822) 07-14 21:29:15.640: E/Database(22231): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:856) 07-14 21:29:15.640: E/Database(22231): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:849) 07-14 21:29:15.640: E/Database(22231): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:556) 07-14 21:29:15.640: E/Database(22231): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 07-14 21:29:15.640: E/Database(22231): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118) 

Any help is much appreciated !! really want a quick fix since the application is already in the play store and I need to post an update

+4
android sqlite android-sqlite sqliteopenhelper
Jul 14 '13 at 17:17
source share
2 answers

The problem was that I was doing these database operations from the widget using asyntasks. Although this works fine on Android 4.0+ devices, however, for small versions of the android, I had to first create activity and do all the database creation and filling in the tables in the application, and then get the widget and display this data.

0
Oct 22 '13 at 10:16
source share

Not sure if it's a kind of “best practice”, but in my application I never keep db open. I open and close it before / after almost every CRUD action. It runs on Android 2.2.

0
Jul 29 '13 at 11:04 on
source share



All Articles