问题描述
所以我做了测试在Android 4.0 +我的code和思考它会正常工作的其他版本的一大失误。但我面临着2.x和3.x问题与SQLiteOpenHelper。
So i made a big mistake of testing my code on android 4.0 + and thinking it would work fine on other versions. But i am facing issues with 2.X and 3.X with SQLiteOpenHelper.
首先,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
通过调用创建DB
Creating the DB by calling
MatchesDB newdb = new MatchesDB(context);
newdb.fillinfotable();
这一切,在所有4.0 +设备完美的工作。但是我越来越想在2.x的设备上运行时出现以下错误
All this is working perfectly on all 4.0 + devices. However i'm getting the following error when trying to run on 2.x devices
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)
任何帮助高度AP preciated!真的想速战速决的应用程序已经在Play商店,我需要发布更新
Any help is Highly appreciated !! really want a quick fix as the app is already on the play store and i need to publish an update
推荐答案
问题是,我是使用asyntasks一个部件做这些数据库操作。然而,虽然这个完美的作品在Android 4.0 +设备,对于较小的Android版本,我不得不首先创建活动,并完成所有的数据库创建和应用填补了表,然后让小部件检索和显示数据。
The issue was that i was doing these database operations from a widget using asyntasks. While this works perfectly on android 4.0 + devices, however for lesser versions of android i had to first create an activity and do all the database creation and filling up of tables in the application and then have the widget retrieve and display that data.
这篇关于问题与Android 2.x和3.x SQLiteOpenHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!