[英]Delete old table and create new one and prepopulate it - RoomDatabase
I am creating a simple android application in android studio with java .我正在使用 java 在 android 工作室中创建一个简单的android应用程序。 I have a roomdatabase db with a table named user that has four columns name, profession, age, description and I have prepopulated using a database that i made in sqlite studio.我有一个 roomdatabase db,其中有一个名为 user 的表,该表有四列名称、职业、年龄、描述,我已经使用我在 sqlite 工作室制作的数据库进行了预填充。
Now, I want to add a column surname on the table but I want to delete all the prepopulated data and prepopulate again the table with a new database that contains also surname.现在,我想在表中添加一个姓氏列,但我想删除所有预填充的数据,并使用还包含姓氏的新数据库再次预填充表。
At first I thought to use auto migrations and to just add a new column.起初我想使用自动迁移并只添加一个新列。 But i don't know how delete all the existing data and prepopulate again the database.但我不知道如何删除所有现有数据并再次预填充数据库。
I want to delete the existing data because i want to change all the info that exists in the column description .我想删除现有数据,因为我想更改列描述中存在的所有信息。 Also as concern as the column name now it contains the fullname and in some cases is written line "name surname" and other times like "surname name" ex "Will Smith" "Smith Will".同样关注列名,现在它包含全名,在某些情况下写成行“name surname”,其他时候像“surname name”ex“Will Smith”“Smith Will”。 Now I want to have the name and the surname in separate columns name and surname现在我想把名字和姓氏放在不同的列名和姓氏中
Could someone recommend me something?有人可以推荐我一些东西吗? Thank you in advance先感谢您
AutoMigration will not cope with amending the data itself. AutoMigration 不会处理修改数据本身。 Thus you will have to do that manually and thus use a destructive migration.因此,您将不得不手动执行此操作,从而使用破坏性迁移。
Here's a example (as can be seen actually undertaken)这是一个例子(可以看出实际进行)
Note this assumes no version or version 1 was assigned to the original pre-populated database and also that 1 was used for the version passed to the @Database annotation.请注意,这假设没有版本或版本 1 分配给原始预填充的数据库,并且 1 用于传递给 @Database 注释的版本。
an SQLite database has, as part of it's header, a user_version number (offset 60 for 4 bytes).作为 header 的一部分,一个 SQLite 数据库有一个 user_version 号(偏移 60 为 4 个字节)。 It is comparing this to the version passed to Room that determines the migration/auto migration.它将此与传递给 Room 的版本进行比较,以确定迁移/自动迁移。 As is seen changing this is critical if migrating.正如所见,如果迁移,改变这一点是至关重要的。
If developing you could just start from the changed database by making the changes to the database and the App and uninstalling the App.如果正在开发,您可以通过对数据库和应用程序进行更改并卸载应用程序来从更改后的数据库开始。 No need to play with version numbers.无需玩版本号。
:- :-
@Entity
class User {
@PrimaryKey
@NonNull
String name;
String profession;
int age;
String description;
/* ADDED FOR V2 */
String surname;
}
Compile (Ctrl + F9) and then from Android View located the generated Java and then the class that is then same as the @Database class but suffixed with _Impl .编译(Ctrl + F9),然后从 Android View 找到生成的 Java,然后找到与 @Database class 相同但后缀为 _Impl 的class 。
In SQLite Studio, run the following SQL:- ALTER TABLE user ADD COLUMN surname TEXT;
在 SQLite Studio 中,运行以下 SQL:- ALTER TABLE user ADD COLUMN surname TEXT;
where the text/code after the COLUMN key word is EXACTLY as per the SQL noted above (you can include or omit the enclosing `'s around the column name, they aren't easy to display in SO )其中 COLUMN 关键字后的文本/代码完全按照上面提到的 SQL(您可以包括或省略列名称周围的封闭 `,它们不容易在 SO 中显示)
Look at the Data eg it will now be:-查看数据,例如现在将是:-
Edit the data accordingly.相应地编辑数据。 eg :-例如:-
Then run the following SQL PRAGMA user_version;
然后运行以下 SQL PRAGMA user_version;
(to check the current version) (查看当前版本)
Then run the following SQL PRAGMA user_version = 2;
然后运行以下 SQL PRAGMA user_version = 2;
( to change the version (guessing 2)) (更改版本(猜测2))
Then run the following SQL PRAGMA user_version;
然后运行以下 SQL PRAGMA user_version;
(to check that the version is now 2) (检查版本现在是 2)
Quit SQLite Studio退出 SQLite Studio
Replace the file/asset in the project with the new database.用新数据库替换项目中的文件/资产。 eg :-例如:-
In the @Database class (NOT the generated java):-在@Database class(不是生成的java)中:-
.fallbackToDestructiveMigrationFrom(1)
if going from 1 to 2 .fallbackToDestructiveMigrationFrom(1)
如果从 1 到 2.fallbackToDestructiveMigration()
(not as safe but more encompassing)或.fallbackToDestructiveMigration()
(不那么安全但更具包容性)Run the App and eg :-运行应用程序,例如:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.