Exporting Room Database to csv file in android

kinjal patel picture kinjal patel · Jun 27, 2018 · Viewed 8.6k times · Source

There are many tutorials available for exporting SQLite database to csv file but not enough stuff for exporting from room database.

Using sqlite export reference Exporting SQLite Database to csv file in android parsing each column of row manually for room. Following is my code:

     @Dao
     interface CategoryDao {
         @Query("SELECT * FROM Category")
         fun getAllCategory(): List<Category>
     }

//   Export csv logic

      val categoryList = categoryDao.getAllCategory()
      val csvWrite = CSVWriter(FileWriter(file))

      for (item in categoryList) {
         val arrStr = arrayOf<String>(item.categoryId, item.categoryName)
         csvWrite.writeNext(arrStr)
      }

Is there any other way to export csv. Even in room not getting columns name of table pragmatically so not able to create dynamically common logic for all table.

Answer

GreenFlash picture GreenFlash · Jun 27, 2018

This might be Answer that you are searching.

Link: https://stackoverflow.com/a/49133574/9994604

There is also a Manual way to do it. Export Database,

Link: https://stackoverflow.com/a/6542214/9994604

Open in Sqlite Browser: https://sqlitebrowser.org (Free)

Export in CSV/JSON,SQL whichever you like.