Room database Export/Import CSV on Android

Save

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/

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


4 comments

  • Zandro Zulueta
    count is always 0 in if (count == 0). Can you check if your logic is correct? i am trying to read a csv file now and putting it into my Room dB
    • Zandro Zulueta
      count = 1 should be after the for loop in order to read the values in the file. count =0 indicates that it is reading the first line with the column names
    • Zandro Zulueta
      count =1 should be added after the for loop. count =1 indicates it can now read values in lines 2 and above while count = 0 indicates it is reading the column headers
      • Armitage Shanks
        I believe that the error is in line 15 which reads
        for (int i = 0; i < curCSV.getColumnCount() – 1; i++)
        so that it never reads the last column

        and should read either

        for (int i = 0; i < curCSV.getColumnCount() ; i++)

        OR

        for (int i = 0; i <= curCSV.getColumnCount() – 1; i++)

  • css.php

    Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.

    Osama
    Talk to Sales

    Global

    Live Chat
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home