所以我犯了一个大错误,在Android 4.0 +上测试我的代码,并认为它可以在其他版本上正常工作。但我面临的问题是2.X和3.X与SQLiteOpenHelper。
首先是代码:
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
通过调用
创建数据库 MatchesDB newdb = new MatchesDB(context);
newdb.fillinfotable();
所有这一切都适用于所有4.0 +设备。但是,当我尝试在2.x设备上运行时,我收到以下错误
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)
任何帮助都非常感谢!!真的想快速解决,因为该应用已经在Play商店,我需要发布更新
答案 0 :(得分:0)
不确定它是否是一种“最佳实践”,但在我的应用程序中,我从不保持数据库打开。我在几乎每个CRUD动作之前/之后打开和关闭它。它适用于Android 2.2。
答案 1 :(得分:0)
问题是我使用asyntasks从一个小部件进行这些数据库操作。虽然这在Android 4.0 +设备上运行得很好,但是对于较小版本的android,我必须先创建一个活动,然后在应用程序中创建所有数据库并填充表格,然后让小部件检索并显示该数据。