简体   繁体   English

删除旧表并创建新表并预填充它 - RoomDatabase

[英]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.无需玩版本号。

  1. Amend the User class by adding the new surname column.通过添加新的姓氏列来修改用户 class。 eg例如

:- :-

@Entity
class User {
    @PrimaryKey
    @NonNull
    String name;
    String profession;
    int age;
    String description;
    /* ADDED FOR V2 */
    String surname;
}
  1. 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

    1. locate the createAllTables method and then the SQL for the User table.找到createAllTables方法,然后找到 User 表的 SQL。 Make a note of the SQL and the definition for the new column eg -surname TEXT记下 SQL 和新列的定义,例如 -surname TEXT
  2. 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 中显示

  3. Look at the Data eg it will now be:-查看数据,例如现在将是:-

    1. 在此处输入图像描述
  • note that the surname column is populated with nulls.请注意,姓氏列填充了空值。
  1. Edit the data accordingly.相应地编辑数据。 eg :-例如:-

    1. 在此处输入图像描述
  2. Then run the following SQL PRAGMA user_version;然后运行以下 SQL PRAGMA user_version; (to check the current version) (查看当前版本)

  3. Then run the following SQL PRAGMA user_version = 2;然后运行以下 SQL PRAGMA user_version = 2; ( to change the version (guessing 2)) (更改版本(猜测2))

  4. Then run the following SQL PRAGMA user_version;然后运行以下 SQL PRAGMA user_version; (to check that the version is now 2) (检查版本现在是 2)

  5. Quit SQLite Studio退出 SQLite Studio

  6. Replace the file/asset in the project with the new database.用新数据库替换项目中的文件/资产。 eg :-例如:-

    1. 在此处输入图像描述
  7. In the @Database class (NOT the generated java):-在@Database class(不是生成的java)中:-

    1. change the database version to 2将数据库版本更改为 2
    2. add the following to the the databaseBuild:-将以下内容添加到数据库构建中:-
      1. either .fallbackToDestructiveMigrationFrom(1) if going from 1 to 2 .fallbackToDestructiveMigrationFrom(1)如果从 1 到 2
      2. or .fallbackToDestructiveMigration() (not as safe but more encompassing).fallbackToDestructiveMigration() (不那么安全但更具包容性)
  8. Run the App and eg :-运行应用程序,例如:-

    1. 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表中的JavaFX旧单元格与新单元格一起显示 - JavaFX old cells in table are shown with new one 我如何创建新的ArrayList <ArrayList<Integer> &gt;在不更改旧版本的情况下从旧版本中删除? - How can I create new ArrayList<ArrayList<Integer>> from an old one without changing the old one? 我应该创建一个新窗口还是修改旧窗口? - Should I create a new window or modify the old one? 休眠中的更新方法不会替换旧数据,而是创建一个新数据吗? - The update method in hibernate does not replace the old data but create a new one? 如何从旧实例创建新实例? - How do I create a new instance from an old one? 如何从 x 文档中获取数据并将其上传到名为“名称”的新文档中,然后删除 java 中的旧文档 - How to get the data from x document and upload it to a new document called 'name' and then delete the old one in java Spring 工具套件在删除旧项目后不在浏览器中显示新项目 - Spring Tool Suite doesn't show new project in browser after delete the old one 用旧的覆盖新的软件包 - override the new package with the old one 用新的替换旧的JButton - replace old JButton with new one 应用程序上的旧的新“删除/无效” Cookie - Old new “delete/invalidade” Cookie on an application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM