Start a Project

Room database Export/Import CSV on Android

In this blog, I am sharing my experience with the room database to export and import the data into the CSV file.

CSV is a simple file format used to store tabular data, such as a spreadsheet or database. Files in the CSV format can be imported to and exported from programs that store data in tables, such as Microsoft Excel or OpenOffice Calc. CSVstands for “comma-separated values”.

This is a very powerful database which is based on ORM.  The room database provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

The library helps you create a cache of your app’s data on a device that’s running your app. This cache, which serves as your app’s single source of truth, allows users to view a consistent copy of key information within your app, regardless of whether users have an internet connection.

If you are a newbie in a room database then first you have to read these below blogs,

Android Room Persistence Library


Android Basic Structure – Kotlin and Room Database android

So let’s continue to further without any more introduction about Room database.

Export Database to CSV file

This is a simple and straight forward method for export data into CSV,

Note: You can take the CSVWriter file from this URL: https://github.com/rogerta/secrets-for-android/tree/master/app/src/main/java/au/com/bytecode/opencsv 

Import Database from CSV file

For  importing, I am dividing this section into 2 parts,

  1. Extract data from CSV file
  2. Put that data into the database

Extract Data from CSV file

Put that data into the Database

We are further dividing into two parts,

A basic implementation of SupportSQLiteQuery which receives a query and its args and binds args based on the passed in Object type.

That’s done. Now you are able to export your database in CSV file and import from CSV.

 

Reference: The job saver of every developer 🙂 – https://stackoverflow.com/

Exit mobile version