Create Android Room entity classes from DB file

Viewed 2146

I have a database file and queries. Using this I want to generate Room Entities. Is there any tool available using which I can automatically generate room entities?

2 Answers

There is no specific tools/plugins I remember which can handle the whole process altogether.

But yes, you can reduce efforts by the following process-

  • Go to Android Studio -> File -> Settings -> Plugins
  • Search for "Room Table Builder" , install it and restart Android Studio.
  • You have database file with you, have some data installed on each of your tables. Now open the database file with DB browser for SQLite.
  • Go to Db browser for SQLite File -> Export -> Table(s) to JSON...
  • Select all Tables and click Ok. After this, you will have to select the folder where you want to save all the JSON files. Do it.
  • Now, open any .json ouput file and copy the content.
  • Go to Android Studio, Right click on the package where you want to create Room Entity.
  • Go in the new option "Create Room Model from JSON"
  • Paste the copied .json file content in the block.
  • Provide a Table name and click "Create Room Table".
  • Done! You have created the Entity class for a Database Table. Repeat the copy and paste process for all the .json file, you had created from "DB Browser for SQLite".

There is an Android Studio plugin: SQLScout

SQLScout also features:

  1. The ability to connect to databases in Android devices and the file system
  2. A database schema explorer that displays the structure of databases
  3. A SQL editor that provides all the features you’d expect from an IDE (syntax highlighting, code completion, reference navigation and refactoring) and the ability to execute SQL statements
  4. A database console to view query results, edit table data, and export data to different formats (including Excel)
  5. Database diagrams

You can refer this blog for full steps.

Related