我有这样的一个需求:整个项目的模块是分别在多个独立的lib工程中,通过挂载整合成一个完整的项目,我希望整个项目只有一个DB,可是我在不同模块(也就是不同lib工程中)分别写了各自的DBHelper(都继承SQLiteOpenHelper),并且在DBHelper的构造方法中创建DB时都使用了同一个DB名,但是每个模块都创建各自的表,结果是DB中只有最后一个被调用的模块的表,之前的表都没了,请问是为什么呢?该如何实现这种需求?
位于工程1中的DBHelper1:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper1 extends SQLiteOpenHelper {
public static final String TAG = GCMDBHelper.class.getSimpleName();
private static final String DATABASE_NAME = "DB.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "TB_NOTIFICATION";
private static final String ID = "ID";
private static final String NOTIFICATION_ID = "NOTIFICATION_ID";
private static final String MESSAGE = "MESSAGE";
private static final String EXTRAS = "EXTRAS";
private static final String TIME = "TIME";
public DBHelper1(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d(TAG, "Create DB: [Name:" + DATABASE_NAME + ", Version:" + DATABASE_VERSION + "]");
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID
+ " integer PRIMARY KEY AUTOINCREMENT," + NOTIFICATION_ID
+ " integer," + MESSAGE + " text," + EXTRAS
+ " text," + TIME + " integer);";
Log.d(TAG, sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "DATABASE needs upgrade. oldVersion:" + oldVersion + ", newVersion:" + newVersion);
Log.d(TAG, "DROP TABLE IF EXISTS: [TABLE_NAME:" + TABLE_NAME + "]");
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
位于工程2中的DBHelper2:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper2 extends SQLiteOpenHelper {
public static final String TAG = GCMDBHelper.class.getSimpleName();
private static final String DATABASE_NAME = "DB.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "TB_LOCAL_NOTIFICATION";
private static final String ID = "ID";
private static final String TITLE = "TITLE";
private static final String TEXT = "TEXT";
private static final String HOUR = "HOUR";
private static final String MINUTE = "MINUTE";
private static final String ICON_RESOURCE_ID = "ICON_RESOURCE_ID";
private static final String REPEAT = "REPEAT";
private static final String REQUEST_CODE = "REQUEST_CODE"; // 业务上唯一
private static final String REPEAT_TIME = "REPEAT_TIME";
private static final String DELAY = "DELAY";
private static final String YEAR = "YEAR";
private static final String MONTH = "MONTH";
private static final String DAY = "DAY";
public DBHelper2(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d(TAG, "Create DB: [Name:" + DATABASE_NAME + ", Version:" + DATABASE_VERSION + "]");
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID
+ " integer PRIMARY KEY AUTOINCREMENT," + TITLE + " text,"
+ TEXT + " text," + HOUR + " integer," + MINUTE + " integer,"
+ ICON_RESOURCE_ID + " integer," + REPEAT + " integer,"
+ REQUEST_CODE + " integer," + REPEAT_TIME + " integer,"
+ DELAY + " integer," + YEAR + " integer," + MONTH
+ " integer," + DAY + " integer);";
Log.d(TAG, sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "DATABASE needs upgrade. oldVersion:" + oldVersion + ", newVersion:" + newVersion);
Log.d(TAG, "DROP TABLE IF EXISTS: [TABLE_NAME:" + TABLE_NAME + "]");
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
结果是只有DB中只有TB_LOCAL_NOTIFICATION一张表,但如果DB名字选用不同的,就会创建2个DB,分别有各自的表。
我的做法是每个DBHelper里单独写一个建表的方法,再做一个查表验证,在开发需要时分别调用这些方法。
等最后发布时,再写到第一个DBHelper里面
找到问题所在了,SQLiteDatabase应该是单例的,一个application中只有一个SQLiteDatabase对象,即便我这里有多个DBHelper,但是只有第一个被实例化的DBHelper才会执行其onCreate方法,之后被实例化的DBHelper的onCreate不会执行,所以其中的表也没有创建,解决方法是将建表语句放在onOpen方法中,该方法是每次打开DB时都会执行的。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。