How To Import And Export SQLite Database

In this article, we will learn how to import/export Android SQLite Database directly from an application. We can use an SQLiteImporterExporter library to perform these functionalities.

Android

Introduction

In this article, we will learn how to import/export Android SQLite Database directly from an application. We can use an SQLiteImporterExporter library to perform these functionalities.

SQLite Importer Exporter

It is a lightweight Library to Import or Export SQLite Database from or to External Storage and it is open sourced on GitHub. You can find the Library on GitHub. It is small in size and its size is around “19kb” only.

Steps

I have divided this implementation into 4 steps as shown in the following.

Step 1 - Creating New Project with Android Studio

Step 2 - Setting up the library and AndroidManifest for the project

Step 3 - Creating SQLite Database  

Step 4 - Implementation of the Library

Without any more introduction, we will jump into the coding part.

Step 1: Creating New Project with Android Studio

  1. Open Android Studio and Select Create a new project.
  2. Name the project as you wish and select your activity template.

    Android
  1. Click finish button to create a new project in Android Studio.

Step 2: Setting up the library and AndroidManifest for the project

  1. Open your app level build.gradle file and add the SQLiteToExcel library using the following line.
    1. compile 'com.ajts.androidmads.sqliteimpex:library:1.0.0' 
  1. Then click “Sync Now” to add the library.
  2. Now open your Manifest File (AndroidManifest.xml) and the following permission.
    1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

Step 3: Creating SQLite Database

  1. We have aware of the usage of SQLite in Android. If you want to know how to implement SQLite operations, click here.
  2. The given codes in the link show how to create an implementation of SQLite in Android with SQLiteOpenHelper and how to perform the DB operations with SQLite. Above code will generate SQLite Database and tables.

Step 4 - Implementation of Library

  1. Open your java or any Activity to which you want to add the functionality and add the following lines to initialize the library. By using this, you can export the file to the default location.
    1. SQLiteImporterExporter sqLiteImporterExporter = new SQLiteImporterExporter(getApplicationContext(), "helloworld.db");  
  1. If you want to export the file in user preferred path, use the following.
    1. SqliteToExcel sqliteToExcel = new SqliteToExcel(this"helloworld.db", directory_path); 
    Here, “helloworld.db” is the name of the SQLite Database created in Android Application.
  1. The library has the following listeners for Exporting and Importing in Android.
    1. // Listeners for Import and Export DB  
    2. sqLiteImporterExporter.setOnImportListener(new SQLiteImporterExporter.ImportListener() {  
    3.     @Override  
    4.     public void onSuccess(String message) {  
    5.         Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();  
    6.     }  
    7.   
    8.     @Override  
    9.     public void onFailure(Exception exception) {  
    10.         Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();  
    11.     }  
    12. });  
    13.   
    14. sqLiteImporterExporter.setOnExportListener(new SQLiteImporterExporter.ExportListener() {  
    15.     @Override  
    16.     public void onSuccess(String message) {  
    17.         Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();  
    18.     }  
    19.   
    20.     @Override  
    21.     public void onFailure(Exception exception) {  
    22.         Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();  
    23.     }  
    24. });  

IMPORT FROM ASSETS

Use the following to import DB from assets folder of the application.

  1. try {  
  2.     sqLiteImporterExporter.importDataBaseFromAssets();  
  3. catch (Exception e) {  
  4.     e.printStackTrace();  
  5. }  

IMPORT FROM DIRECTORY

The following lines are used to import DB from user defined or chosen path.

  1. try {  
  2.     sqLiteImporterExporter.importDataBase(path);  
  3. catch (Exception e) {  
  4.     e.printStackTrace();  
  5. }  

EXPORT TO DIRECTORY

The following lines are used to Export DB to user defined or chosen path.

  1. try {  
  2.     sqLiteImporterExporter.exportDataBase(path);  
  3. catch (Exception e) {  
  4.     e.printStackTrace();  
  5. }  

Full Code of the Application

Full code of MainActivity.java,

  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     SQLiteImporterExporter sqLiteImporterExporter;  
  4.     String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";  
  5.     public static String db = "external_db_android.sqlite";  
  6.   
  7.     DbQueries dbQueries;  
  8.   
  9.     EditText edtName;  
  10.     ListView listView;  
  11.     ArrayAdapter<String> adapter;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.   
  18.         sqLiteImporterExporter = new SQLiteImporterExporter(getApplicationContext(), db);  
  19.         sqLiteImporterExporter.setOnImportListener(new SQLiteImporterExporter.ImportListener() {  
  20.             @Override  
  21.             public void onSuccess(String message) {  
  22.                 Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();  
  23.             }  
  24.   
  25.             @Override  
  26.             public void onFailure(Exception exception) {  
  27.                 Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();  
  28.             }  
  29.         });  
  30.         sqLiteImporterExporter.setOnExportListener(new SQLiteImporterExporter.ExportListener() {  
  31.             @Override  
  32.             public void onSuccess(String message) {  
  33.                 Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();  
  34.             }  
  35.   
  36.             @Override  
  37.             public void onFailure(Exception exception) {  
  38.                 Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();  
  39.             }  
  40.         });  
  41.   
  42.         dbQueries = new DbQueries(getApplicationContext());  
  43.   
  44.         edtName = (EditText) findViewById(R.id.edtName);  
  45.         listView = (ListView) findViewById(R.id.listView);  
  46.   
  47.         readDB();  
  48.   
  49.         findViewById(R.id.btnDBExists).setOnClickListener(new View.OnClickListener() {  
  50.             @Override  
  51.             public void onClick(View view) {  
  52.                 if (sqLiteImporterExporter.isDataBaseExists()) {  
  53.                     Toast.makeText(getApplicationContext(), "DB Exists", Toast.LENGTH_SHORT).show();  
  54.                 } else {  
  55.                     Toast.makeText(getApplicationContext(), "DB Doesn't Exists", Toast.LENGTH_SHORT).show();  
  56.                 }  
  57.             }  
  58.         });  
  59.   
  60.         findViewById(R.id.btnImportFromAssets).setOnClickListener(new View.OnClickListener() {  
  61.             @Override  
  62.             public void onClick(View view) {  
  63.                 try {  
  64.                     sqLiteImporterExporter.importDataBaseFromAssets();  
  65.                 } catch (Exception e) {  
  66.                     e.printStackTrace();  
  67.                 }  
  68.                 readDB();  
  69.             }  
  70.         });  
  71.   
  72.         findViewById(R.id.btnExportToExt).setOnClickListener(new View.OnClickListener() {  
  73.             @Override  
  74.             public void onClick(View view) {  
  75.                 try {  
  76.                     sqLiteImporterExporter.exportDataBase(path);  
  77.                 } catch (Exception e) {  
  78.                     e.printStackTrace();  
  79.                 }  
  80.                 readDB();  
  81.             }  
  82.         });  
  83.   
  84.         findViewById(R.id.btnImportFromExt).setOnClickListener(new View.OnClickListener() {  
  85.             @Override  
  86.             public void onClick(View view) {  
  87.                 try {  
  88.                     sqLiteImporterExporter.importDataBase(path);  
  89.                 } catch (Exception e) {  
  90.                     e.printStackTrace();  
  91.                 }  
  92.                 readDB();  
  93.   
  94.             }  
  95.         });  
  96.   
  97.         findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {  
  98.             @Override  
  99.             public void onClick(View view) {  
  100.                 if (sqLiteImporterExporter.isDataBaseExists()) {  
  101.                     if (edtName.getText().toString().trim().length() > 0) {  
  102.                         dbQueries.open();  
  103.                         long success = dbQueries.insertDetail(edtName.getText().toString().trim());  
  104.                         if (success > -1) {  
  105.                             edtName.setText(null);  
  106.                             edtName.clearFocus();  
  107.                             Toast.makeText(getApplicationContext(), "Successfully Inserted", Toast.LENGTH_LONG).show();  
  108.                         } else {  
  109.                             Toast.makeText(getApplicationContext(), "Insertion Failed", Toast.LENGTH_LONG).show();  
  110.                         }  
  111.                         dbQueries.close();  
  112.                         readDB();  
  113.                     } else {  
  114.                         edtName.setError("Enter Name");  
  115.                     }  
  116.                 } else {  
  117.                     edtName.setError("Import DB First");  
  118.                 }  
  119.             }  
  120.         });  
  121.   
  122.     }  
  123.   
  124.     private void readDB() {  
  125.         if (sqLiteImporterExporter.isDataBaseExists()) {  
  126.             dbQueries.open();  
  127.             adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.list_item, dbQueries.getDetail());  
  128.             listView.setAdapter(adapter);  
  129.             dbQueries.close();  
  130.         } else {  
  131.             Toast.makeText(getApplicationContext(), "DB Doesn't Exists", Toast.LENGTH_SHORT).show();  
  132.         }  
  133.     }  
  134. }  

Note

  • If you find any issues in this library, feel free report the issues in GitHub issue
  • You can find the WIKI or documentation for the usage and its features here.

Download Code

You can find the library on GitHub. If you like this library, do star the library in GitHub and share this library. The sample implementation of this library can be found here.