1.1Androidx 地址
versions.room = "2.2.5"
deps.room = "androidx.room:room-runtime:$versions.room"
deps.roomcompiler = "androidx.room:room-compiler:$versions.room"
1.2建表
@Entity(tableName = "user_info")
public class Im_user {
//主键
@PrimaryKey
@NonNull
public String user_id;
@ColumnInfo
public String user_name;
@ColumnInfo
public String user_photo;
@ColumnInfo
public String user_sex;
public Im_user(@NonNull String user_id, String user_name, String user_photo) {
this.user_id = user_id;
this.user_name = user_name;
this.user_photo = user_photo;
}
1.3dao 操作
@Dao
public interface ImUserDao {
@Query("SELECT * FROM user_info")
List<Im_user> getAll();
@Query("SELECT * FROM user_info WHERE user_id = :user_id")
Im_user queryUserByid(String user_id);
@Insert(onConflict = REPLACE)
void insertAll(Im_user... users);
@Delete
void delete(Im_user user);
}
1.4数据配置
@Database(entities = {Im_user.class}, version = 6)
public abstract class DatabaseConfig extends RoomDatabase {
public abstract ImUserDao userDao();
}
1.5
public class DatabaseInitialize {
private static DatabaseConfig databaseConfig;
private DatabaseInitialize() {
}
private static Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE user_info "
+ " ADD COLUMN user_photo TEXT");
}
};
private static Migration MIGRATION_2_3 = new Migration(2, 3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE user_info "
+ " ADD COLUMN user_sex INTEGER NOT NULL DEFAULT 0 ");
}
};
private static Migration MIGRATION_3_4 = new Migration(3, 4) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// 创建临时表
database.execSQL(
"CREATE TABLE IF NOT EXISTS users_new (user_id TEXT NOT NULL, user_name TEXT, user_photo TEXT,user_sex TEXT, PRIMARY KEY(user_id))");
// 拷贝数据
database.execSQL(
"INSERT INTO users_new (user_id, user_name, user_photo,user_sex) SELECT user_id, user_name, user_photo,user_sex FROM user_info");
// 删除老的表
// database.execSQL("DROP TABLE user_info");
// // 改名
// database.execSQL("ALTER TABLE users_new RENAME TO user_info");
//删除列
// ALTER TABLE table_name
// DROP COLUMN column_name
//这种做法是不支持的
// database.execSQL("ALTER TABLE user_info" + " ALTER COLUMN user_sex TEXT NOT NULL");
}
};
private static Migration MIGRATION_4_5 = new Migration(4, 5) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// 创建临时表
database.execSQL(
"CREATE TABLE IF NOT EXISTS users_new2 (user_id TEXT NOT NULL, user_name TEXT, user_photo TEXT,user_sex TEXT, PRIMARY KEY(user_id))");
// 拷贝数据
database.execSQL(
"INSERT INTO users_new2 (user_id, user_name, user_photo,user_sex) SELECT user_id, user_name, user_photo,user_sex FROM user_info");
// 删除老的表
// database.execSQL("DROP TABLE user_info");
// // 改名
// database.execSQL("ALTER TABLE users_new RENAME TO user_info");
//删除列
// ALTER TABLE table_name
// DROP COLUMN column_name
//这种做法是不支持的
// database.execSQL("ALTER TABLE user_info" + " ALTER COLUMN user_sex TEXT NOT NULL");
}
};
private static Migration MIGRATION_5_6 = new Migration(5, 6) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
//将原表改成暂存表
database.execSQL("ALTER TABLE user_info RENAME TO temp_user_info");
// 用原表创建新表
database.execSQL(
"CREATE TABLE IF NOT EXISTS user_info (user_id TEXT NOT NULL, user_name TEXT, user_photo TEXT, user_sex TEXT, PRIMARY KEY(user_id))");
// 拷贝数据
database.execSQL(
"INSERT INTO user_info SELECT * FROM temp_user_info");
// 删除老的表
database.execSQL("DROP TABLE temp_user_info");
//删除列
// ALTER TABLE table_name
// DROP COLUMN column_name
//这种做法是不支持的
// database.execSQL("ALTER TABLE user_info" + " ALTER COLUMN user_sex TEXT NOT NULL");
}
};
private static Migration MIGRATION_1_3 = new Migration(1, 3) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE user_info "
+ " ADD COLUMN user_photo TEXT");
database.execSQL("ALTER TABLE user_info "
+ " ADD COLUMN user_sex INTEGER NOT NULL DEFAULT 0 ");
}
};
public static void init(Context context) {
// 生成数据库实例
synchronized (DatabaseInitialize.class) {
if (databaseConfig == null) {
databaseConfig = Room
.databaseBuilder(context.getApplicationContext(), DatabaseConfig.class, "test1.db")
//是否允许在主线程进行查询
.allowMainThreadQueries()
//数据库创建和打开后的回调
//.addCallback()
//设置查询的线程池
//.setQueryExecutor()
.openHelperFactory(new SafeHelperFactory("123456".toCharArray()))
//room的日志模式
//.setJournalMode()
//数据库升级异常之后的回滚
//.fallbackToDestructiveMigration()
//数据库升级异常后根据指定版本进行回滚
//.fallbackToDestructiveMigrationFrom()
//数据库升级的入口(如果没有配置升级的时候会将数据库内容全部清除)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4,MIGRATION_4_5,MIGRATION_5_6)
.build();
}
}
}
static DatabaseConfig getDatabaseConfig() {
if (databaseConfig == null) {
throw new NullPointerException("DatabaseInitialize.init(context) has not call, remember call this function in your Application.class");
}
return databaseConfig;
}
}
1.6
public final class DatabaseSession {
private DatabaseSession(){}
public static DatabaseConfig get() {
return DatabaseInitialize.getDatabaseConfig();
}
}
1.7数据库加密
implementation "com.commonsware.cwac:saferoom.x:1.3.0"
implementation 'net.zetetic:android-database-sqlcipher:4.4.0@aar'
一行代码搞定
databaseConfig = Room
.databaseBuilder(context.getApplicationContext(), DatabaseConfig.class, "test1")
//是否允许在主线程进行查询
.allowMainThreadQueries()
//数据库创建和打开后的回调
//.addCallback()
//设置查询的线程池
//.setQueryExecutor()
.openHelperFactory(new SafeHelperFactory("123456".toCharArray()))
//room的日志模式
//.setJournalMode()
//数据库升级异常之后的回滚
//.fallbackToDestructiveMigration()
//数据库升级异常后根据指定版本进行回滚
//.fallbackToDestructiveMigrationFrom()
//数据库升级的入口(如果没有配置升级的时候会将数据库内容全部清除)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4,MIGRATION_4_5,MIGRATION_5_6)
.build();
网友评论