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 a New Project with Android Studio
- Step 2 - Setting up the library and AndroidManifest for the project
- Step 3 - Creating an SQLite Database
- Step 4 - Implementation of the Library
Without any more introduction, we will jump into the coding part.
Step 1- Creating a New Project with Android Studio
- Open Android Studio and select Create a new project.
- Name the project as you wish and select your activity template.
- Click the “Finish” button to create a new project in Android Studio.
Step 2: Setting up the library and AndroidManifest for the project
- Open your app level build.gradle file and add the SQLiteToExcel library using the following line.
- compile 'com.ajts.androidmads.sqliteimpex:library:1.0.0'
- Then click “Sync Now” to add the library.
- Now open your Manifest File (AndroidManifest.xml) and the following permission.
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Step 3: Creating an SQLite Database
- We have aware of the usage of SQLite in Android. If you want to know how to implement SQLite operations, click here.
- 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. The above code will generate the SQLite databases and tables.
Step 4 - Implementation of Library
- 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.
- SQLiteImporterExporter sqLiteImporterExporter = new SQLiteImporterExporter(getApplicationContext(), "helloworld.db");
- If you want to export the file in user preferred path, use the following.
- SqliteToExcel sqliteToExcel = new SqliteToExcel(this, "helloworld.db", directory_path);
Here, “helloworld.db” is the name of the SQLite Database created in Android Application.
- The library has the following listeners for Exporting and Importing in Android.
-
- sqLiteImporterExporter.setOnImportListener(new SQLiteImporterExporter.ImportListener() {
- @Override
- public void onSuccess(String message) {
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void onFailure(Exception exception) {
- Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
-
- sqLiteImporterExporter.setOnExportListener(new SQLiteImporterExporter.ExportListener() {
- @Override
- public void onSuccess(String message) {
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void onFailure(Exception exception) {
- Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
IMPORT FROM ASSETS
Use the following to import DB from the assets folder of the application.
- try {
- sqLiteImporterExporter.importDataBaseFromAssets();
- } catch (Exception e) {
- e.printStackTrace();
- }
IMPORT FROM DIRECTORY
The following lines are used to import DB from the user-defined or chosen path.
- try {
- sqLiteImporterExporter.importDataBase(path);
- } catch (Exception e) {
- e.printStackTrace();
- }
EXPORT TO DIRECTORY
The following lines are used to Export DB to a user-defined or chosen path.
- try {
- sqLiteImporterExporter.exportDataBase(path);
- } catch (Exception e) {
- e.printStackTrace();
- }
Full Code of the Application
Full code of MainActivity.java,
- public class MainActivity extends AppCompatActivity {
-
- SQLiteImporterExporter sqLiteImporterExporter;
- String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
- public static String db = "external_db_android.sqlite";
-
- DbQueries dbQueries;
-
- EditText edtName;
- ListView listView;
- ArrayAdapter<String> adapter;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- sqLiteImporterExporter = new SQLiteImporterExporter(getApplicationContext(), db);
- sqLiteImporterExporter.setOnImportListener(new SQLiteImporterExporter.ImportListener() {
- @Override
- public void onSuccess(String message) {
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void onFailure(Exception exception) {
- Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
- sqLiteImporterExporter.setOnExportListener(new SQLiteImporterExporter.ExportListener() {
- @Override
- public void onSuccess(String message) {
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void onFailure(Exception exception) {
- Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
-
- dbQueries = new DbQueries(getApplicationContext());
-
- edtName = (EditText) findViewById(R.id.edtName);
- listView = (ListView) findViewById(R.id.listView);
-
- readDB();
-
- findViewById(R.id.btnDBExists).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (sqLiteImporterExporter.isDataBaseExists()) {
- Toast.makeText(getApplicationContext(), "DB Exists", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(getApplicationContext(), "DB Doesn't Exists", Toast.LENGTH_SHORT).show();
- }
- }
- });
-
- findViewById(R.id.btnImportFromAssets).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- sqLiteImporterExporter.importDataBaseFromAssets();
- } catch (Exception e) {
- e.printStackTrace();
- }
- readDB();
- }
- });
-
- findViewById(R.id.btnExportToExt).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- sqLiteImporterExporter.exportDataBase(path);
- } catch (Exception e) {
- e.printStackTrace();
- }
- readDB();
- }
- });
-
- findViewById(R.id.btnImportFromExt).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- sqLiteImporterExporter.importDataBase(path);
- } catch (Exception e) {
- e.printStackTrace();
- }
- readDB();
-
- }
- });
-
- findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (sqLiteImporterExporter.isDataBaseExists()) {
- if (edtName.getText().toString().trim().length() > 0) {
- dbQueries.open();
- long success = dbQueries.insertDetail(edtName.getText().toString().trim());
- if (success > -1) {
- edtName.setText(null);
- edtName.clearFocus();
- Toast.makeText(getApplicationContext(), "Successfully Inserted", Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(getApplicationContext(), "Insertion Failed", Toast.LENGTH_LONG).show();
- }
- dbQueries.close();
- readDB();
- } else {
- edtName.setError("Enter Name");
- }
- } else {
- edtName.setError("Import DB First");
- }
- }
- });
-
- }
-
- private void readDB() {
- if (sqLiteImporterExporter.isDataBaseExists()) {
- dbQueries.open();
- adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.list_item, dbQueries.getDetail());
- listView.setAdapter(adapter);
- dbQueries.close();
- } else {
- Toast.makeText(getApplicationContext(), "DB Doesn't Exists", Toast.LENGTH_SHORT).show();
- }
- }
- }
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.