Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
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.
This is a simple and straight forward method for export data into CSV,
File exportDir = new File(Environment.getExternalStorageDirectory(), ""); if (!exportDir.exists()) { exportDir.mkdirs(); } File file = new File(exportDir, fileName + ".csv"); try { file.createNewFile(); CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); Cursor curCSV = db.query("SELECT * FROM " + TableName, null); csvWrite.writeNext(curCSV.getColumnNames()); while (curCSV.moveToNext()) { //Which column you want to exprort String arrStr[] = new String[curCSV.getColumnCount()]; for (int i = 0; i < curCSV.getColumnCount() - 1; i++) arrStr[i] = curCSV.getString(i); csvWrite.writeNext(arrStr); } csvWrite.close(); curCSV.close(); ToastHelper.showToast(this, "Exported", Toast.LENGTH_SHORT); } catch (Exception sqlEx) { Log.e("MainActivity", sqlEx.getMessage(), sqlEx); } 123456789101112131415161718192021222324 File exportDir = new File(Environment.getExternalStorageDirectory(), ""); if (!exportDir.exists()) { exportDir.mkdirs(); } File file = new File(exportDir, fileName + ".csv"); try { file.createNewFile(); CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); Cursor curCSV = db.query("SELECT * FROM " + TableName, null); csvWrite.writeNext(curCSV.getColumnNames()); while (curCSV.moveToNext()) { //Which column you want to exprort String arrStr[] = new String[curCSV.getColumnCount()]; for (int i = 0; i < curCSV.getColumnCount() - 1; i++) arrStr[i] = curCSV.getString(i); csvWrite.writeNext(arrStr); } csvWrite.close(); curCSV.close(); ToastHelper.showToast(this, "Exported", Toast.LENGTH_SHORT); } catch (Exception sqlEx) { Log.e("MainActivity", sqlEx.getMessage(), sqlEx); }
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
For importing, I am dividing this section into 2 parts,
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.
SupportSQLiteQuery
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/
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
and should read either
for (int i = 0; i < curCSV.getColumnCount() ; i++)
OR
for (int i = 0; i <= curCSV.getColumnCount() – 1; i++)
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
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
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.