Android应用不会创建数据库

时间:2016-03-08 04:50:17

标签: android database sqlite

我的数据库架构为:

public class Database extends SQLiteOpenHelper {

    public static final String TAG = Database.class.getSimpleName();

    public static final String DATABASE_NAME = "Database";
    public static final int DATABASE_VERSION = 1;

    SaveSharedPreference preference = new SaveSharedPreference();


    public Database(Context context) {
        super(context, Environment.getExternalStorageDirectory() + "/sdcard/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

    static SQLiteDatabase database = null;
    static Database instance = null;

    public static SQLiteDatabase getDatabase() {
        if (null == database) {
            database = instance.getWritableDatabase();
        }
        return database;
    }

    public static void init(Context context) {
        if (null == instance) {
            instance = new Database(context);
        }
    }

    public static void deactivate() {
        if (null != database && database.isOpen()) {
            database.close();
        }
        database = null;
        instance = null;
    }

    // NON-CUSTOM COLUMNS
    public static final String _ID = "id";
    public static final String CREATE_DT = "create_dt";
    public static final String UPDATE_DT = "update_dt";

    // EVENT TABLE
    public static final String TABLE_NAME_EVENTS = "events";
    public static final String EVENT_SYNC_ID = "event_sync_id";
    public static final String EVENT_CREATOR_SYNC_ID = "event_creator_sync_id";
    public static final String EVENT_PREFERENCES = "event_preferences";


    public static final String TABLE_EVENTS_CREATE = "CREATE TABLE "
            + TABLE_NAME_EVENTS + " ("
            + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + EVENT_SYNC_ID + " VARCHAR(255) NOT NULL, "
            + EVENT_CREATOR_SYNC_ID + " VARCHAR(255) NOT NULL, "
            + EVENT_PREFERENCES + " VARCHAR(8000) NOT NULL, "
            + CREATE_DT + " VARCHAR(255), "
            + UPDATE_DT + " VARCHAR(255))";

    // DROPPING TABLE:  Should do instead of truncate if need to refresh all data/
    public static final String DROP_TABLE_PREFIX = "DROP TABLE IF EXISTS ";

    @Override
    public void onCreate(SQLiteDatabase db) {

        // CREATE TABLES DEFINED ABOVE
        db.execSQL(DROP_TABLE_PREFIX + TABLE_NAME_EVENTS);

        onCreate(db);
    }

和MyApplicaton类,在app start上创建数据库:

public void onCreate() {
    super.onCreate();

    Database DB = new Database(this);
    SQLiteDatabase db = DB.getWritableDatabase();

}

我尝试使用上面列出的代码,但是查看文件路径/sdcard/,没有创建数据库。

如何创建此数据库并确保创建它?

1 个答案:

答案 0 :(得分:1)

You can only create database in onCreate() Method

@Override
    public void onCreate(SQLiteDatabase db) {

        // CREATE TABLES DEFINED ABOVE
        db.execSQL(TABLE_NAME_EVENTS);

        onCreate(db);
    }

for Dropping table use 

@Override
    public void onUpgrade(SQLiteDatabase db) {

        // DroppingTABLES DEFINED ABOVE
        db.execSQL(DROP_TABLE_PREFIX );

        onCreate(db);
    }