开发者社区> 问答> 正文

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,分别有各自的表。

展开
收起
爵霸 2016-06-07 09:08:56 2916 0
2 条回答
写回答
取消 提交回答
  • 我的做法是每个DBHelper里单独写一个建表的方法,再做一个查表验证,在开发需要时分别调用这些方法。
    等最后发布时,再写到第一个DBHelper里面

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

    2019-07-17 19:29:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android内存泄漏自动化链路分析组件Probe 立即下载
微信SQLite数据库损坏恢复实践 立即下载