Android SQLiteOpenHelper的新实例是否在app目录中创建了一个新数据库?

时间:2015-07-09 06:40:50

标签: java android sqlite

我在android中创建了一个数据库助手类,我关心的是每次创建Helper类的新实例时,android系统是否会创建一个覆盖所有以前数据的新数据库?

public class UserHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;

    public static final String DATABASE_NAME = "user.db";

    public UserHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        final String SQL_CREATE_USER_PROFILE_TABLE = "CREATE TABLE " + UserProfileEntry.TABLE_NAME +
                " (" +
                UserProfileEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                UserProfileEntry.COLUMN_NAME_PARENT_NAME + " TEXT NOT NULL, " +
                UserProfileEntry.COLUMN_NAME_PARENT_PHONE + " INTEGER UNIQUE NOT NULL, " +
                UserProfileEntry.COLUMN_NAME_PARENT_EMAIL + " TEXT UNIQUE NOT NULL, " +
                UserProfileEntry.COLUMN_NAME_PARENT_PASSWORD + " TEXT NOT NULL, " +
                UserProfileEntry.COLUMN_NAME_CHILD_NAME + " TEXT NOT NULL, " +
                UserProfileEntry.COLUMN_NAME_CHILD_DOB + " TEXT NOT NULL, " +
                UserProfileEntry.COLUMN_NAME_CHILD_GENDER + " TEXT NOT NULL, " +
                " UNIQUE ( " + UserProfileEntry.COLUMN_NAME_PARENT_PHONE + ", " +
                UserProfileEntry.COLUMN_NAME_PARENT_EMAIL + " ) ON CONFLICT IGNORE " +
                ");";

        final String SQL_CREATE_ALL_ACTIVITIES_TABLE = "CREATE TABLE " + AllActivitiesEntry.TABLE_NAME +
                " ( " +
                AllActivitiesEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                AllActivitiesEntry.COLUMN_NAME_ACTIVITY_NAME + " TEXT UNIQUE NOT NULL, " +
                AllActivitiesEntry.COLUMN_NAME_MIN_AGE + " INTEGER NOT NULL );";

        final String SQL_CREATE_USER_ACTIVITIES_TABLE = "CREATE TABLE " +
                UserActivitiesEntry.TABLE_NAME + " (" +
                UserActivitiesEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                UserActivitiesEntry.COLUMN_NAME_USER_ID + " INTEGER NOT NULL, " +
                UserActivitiesEntry.COLUMN_NAME_USER_ACTIVITY_ID + " INTEGER NOT NULL, " +

                //Setting up COLUMN_USER_ID as a foreign key to UserProfileTable
                " FOREIGN KEY ( " + UserActivitiesEntry.COLUMN_NAME_USER_ID + " ) REFERENCES " +
                UserProfileEntry.TABLE_NAME + " ( " + UserProfileEntry._ID + " ), " +

                //Setting up COLUMN_USER_ACTIVITY_ID as a foreign key to AllActivitiesTable
                " FOREIGN KEY ( " + UserActivitiesEntry.COLUMN_NAME_USER_ACTIVITY_ID +
                " ) REFERENCES " + AllActivitiesEntry.TABLE_NAME +
                " ( " + AllActivitiesEntry._ID + " ) );";

        db.execSQL(SQL_CREATE_USER_PROFILE_TABLE);
        db.execSQL(SQL_CREATE_ALL_ACTIVITIES_TABLE);
        db.execSQL(SQL_CREATE_USER_ACTIVITIES_TABLE);
    }

    public static boolean checkDatabaseExists(Context context, String databaseName) {
        File dbFile = context.getDatabasePath(databaseName);
        return dbFile.exists();
    }
}

1 个答案:

答案 0 :(得分:2)

数据库不存在,它将创建一个,并在构造函数中指定版本号。 Android然后会调用onCreate()方法来创建表。

如果数据库确实存在,则将使用旧版本号和新版本号调用onUpgrade()。这允许您为应用程序的旧版本上的用户添加缺少的表,并且正在升级到更新的版本。