Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – How to import and manage conflicts an excel file into room sqlite database?

Posted on November 11, 2022 By

Solution 1 :

It would be nice to look on the following articles. Article1 and article2 focuses on using room to import csv using room.

If you can use a different library, this is also helpful.

I will advise to break this functionality into two tasks.

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

1 To extract data from CSV/Excel File, you can use the following code.

    CSVReader csvReader = new CSVReader(new FileReader(Environment.getExternalStorageDirectory() + "/" + TableName));
String[] nextLine;
int count = 0;
StringBuilder columns = new StringBuilder();
StringBuilder value = new StringBuilder();
 
while ((nextLine = csvReader.readNext()) != null) {
      // nextLine[] is an array of values from the line
         for (int i = 0; i < nextLine.length - 1; i++) {
             if (count == 0) {
                 if (i == nextLine.length - 2)
                     columns.append(nextLine[i]);
                 else
                     columns.append(nextLine[i]).append(",");
             } else {
                 if (i == nextLine.length - 2)
                    value.append("'").append(nextLine[i]).append("'");
                 else
                    value.append("'").append(nextLine[i]).append("',");
             }
        }
  1. To put the extracted data into the database you can use the following code.

Create a raw query function in your DAO.

@RawQuery  
Boolean insertDataRawFormat(SupportSQLiteQuery query);

Using that query put the data into the Database

SimpleSQLiteQuery query = new SimpleSQLiteQuery("Insert INTO " + tableName + " (" + columns + ") " + "values(" + value + ")",
           new Object[]{});
    getDb().cashDrawerDao().insertDataRawFormat(query);

Problem :

I want to import an excel file into my database designed with room library on android.
I can export the database using a library but for import i need to check items and handle conflicts but I don’t know how.

Comments

Comment posted by Community

Please provide enough code so others can better understand or reproduce the problem.

READ  [FIXED] android - SQLite crashes from big image stored in database
Powered by Inline Related Posts
Android Tags:android, android-room, database, java, sqlite

Post navigation

Previous Post: [FIXED] React-native android build fails due to missing files in the gradle cache?
Next Post: Can I use Google storage instead of iCloud?

Related Posts

[FIXED] kotlin – Android MVVM 2 ViewModels Sharing 1 Repository with LiveData Android
[FIXED] dart – how to get list of colors in android studio for mac? (ctrl q on windows) Android
[FIXED] android – ViewPager2 with Fragment data set change Android
[FIXED] android – Google Photos app bottom navigation bar behavior Android
[FIXED] java – Sending http request get syntax error 400 message from API on Android Android
[FIXED] android – React Native – ERROR point to the same directory in the file system. Each module must have a unique path Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Is the Nissan NV 2500 reliable?
  • Quel est le fromage qui porte le nom d’une commune de Côte d’Or et qui est produit en Haute-marne ?
  • Welches ist das beste Senioren Smartphone?
  • What is ISO benefit?
  • What do red bottom shoes represent?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme