Code Monkey home page Code Monkey logo

sqlite2xl's Introduction

.github/workflows/main.yml Quality Gate Status

SQLite2XL

A HMOS library to convert SQLite Database to Excel and Convert Excel to SQLite.

Source

Inspired by androidmads/SQLite2XL - version v1.0.2

Features

This library provides user the functions to export SQLite Database to Excel and import Excel data to SQLite database.

Usage of library in Sample App

  1. This sample application has two buttons, user can choose SQLite 2 Excel and Excel 2 SQLite.
  2. When the user clicks on the button SQLite 2 Excel, user gets to add the name and phone number of a person.
  3. User has to save the entered data by clicking the Save button, the data which is saved is displayed on the screen.
  4. User has to click on Export button to export the data to the desired xml file.
  5. Then User has to click on Excel 2 SQLite button and import the data from the xml file to database.
  6. User can now click on SQLite 2 Excel to check if the data is imported.
  7. Export with Exclude button will export the data to another xml file excluding the contact number entered by the user.

Exporting from SQLite to Excel

Importing from Excel into Database

Dependency

  1. For using sqlite2xl module in sample app, include the source code and add the below dependencies in entry/build.gradle to generate hap/support.har.
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
    implementation project(path: ':sqlite2xl')
    testImplementation 'junit:junit:4.13'
    ohosTestImplementation 'com.huawei.ohos.testkit:runner:1.0.0.100'
}
  1. For using sqlite2xl in separate application using har file, add the har file in the entry/libs folder and add the dependencies in entry/build.gradle file.
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.har'])
    testImplementation 'junit:junit:4.13'
}
  1. For using sqlite2xl from a remote repository in separate application, add the below dependencies in entry/build.gradle file.
dependencies {
    implementation 'dev.applibgroup:sqlite2xl:1.0.0'
    testImplementation 'junit:junit:4.13'
}

Usage

Export SQLite to Excel

This line is used to save the exported file in used preferred location.

SqLiteToExcel sqliteToExcel = new SqLiteToExcel(getApplicationContext(), DbHelper.DB_NAME, directoryPath);

This code snippet is used to Export every table in a database to Excel Sheet

sqliteToExcel.exportAllTables("users.xls", new SqLiteToExcel.ExportListener() {
    @Override
    public void onStart() {
        // Do nothing.
    }

    @Override
    public void onCompleted(String filePath) {
        Utils.showSnackBar(component, "Successfully Exported");
    }

    @Override
    public void onError(Exception e) {
        Utils.showSnackBar(component, e.getMessage());
    }
});

This code snippet shows how to exclude columns from the export (the resulting export file may not be able to be imported).

List<String> excludeColumns = new ArrayList<>();
excludeColumns.add("contact_id");
sqliteToExcel.setExcludeColumns(excludeColumns);
...
sqliteToExcel.export...

This code snippet shows how to pretty names (either sheet names or column names (the resulting export file may not be able to be imported).

 Map<String, String> prettyNames = new HashMap<>();
prettyNames.put("contact_person_name", "Person Name");
prettyNames.put("contact_no", "Mobile Number");
sqliteToExcel.setPrettyNameMapping(prettyNames);
...
sqliteToExcel.export.. 

This code snippet shows how to format the value for a column on export.

sqliteToExcel.setCustomFormatter(new SqLiteToExcel.ExportCustomFormatter() {
    @Override
    public String process(String columnName, String value) {
        String contactPersonName = "contact_person_name";
        if (columnName.equals(contactPersonName)) {
            value = "Sale";
        }
        return value;
    }
});

Import Excel into Database

The following snippet is used to initialize the library for Importing Excel.

ExcelToSqLite excelToSqlite = new ExcelToSqLite(getApplicationContext(), DbHelper.DB_NAME, false);

The following code is used to Import Excel from user directory

excelToSqlite.importFromFile(directoryPath, new ExcelToSqLite.ImportListener() {
    @Override
    public void onStart() {
        // Do nothing.
    }

    @Override
    public void onCompleted(String dbName) {
        Utils.showSnackBar(component, "Excel imported into " + dbName);
    }

    @Override
    public void onError(Exception e) {
        Utils.showSnackBar(component, "Error : " + e.getMessage());
    }
});

Future Work

Current sample app exports file into application data partition and also imports from the same file into database. As an extension, Export and import can be tried with external storage paths and with customizable file paths.

Licence

MIT License

Copyright (c) 2017 AndroidMad / Mushtaq M A

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

sqlite2xl's People

Contributors

arpithakori avatar gleelakrishna avatar applibengineering avatar

Watchers

Sharath KS avatar

Forkers

arpithakori

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.