Android SQLite同一DB中创建多表

我有这样的一个需求:整个项目的模块是分别在多个独立的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,分别有各自的表。

1个回答

找到问题所在了,SQLiteDatabase应该是单例的,一个application中只有一个SQLiteDatabase对象,即便我这里有多个DBHelper,但是只有第一个被实例化的DBHelper才会执行其onCreate方法,之后被实例化的DBHelper的onCreate不会执行,所以其中的表也没有创建,解决方法是将建表语句放在onOpen方法中,该方法是每次打开DB时都会执行的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问