Loading an externally created Sqlite database

Asked

Viewed 1,125 times

0

I need to use a previously created Sqlite database, already with records inserted in it. But the only way I had done until then was when the application itself created the bank, which I internally populated in the application.

Now I have a Sqlite bank with considerable records and need to use it in my application, but I’m not able to do it. I looked this tutorial, but I was unsuccessful. While running the application, it always triggered an exception with "Could not copy the file".

I would like to know a way to use my bank, Sqlite, created externally in my Android application, do not need to follow the principles of the tutorial above.

  • Did you make the file available where your application would create it? and gave read and write permission?

4 answers

1

In Android Studio 4.1 this operation can be performed using the Device File Explorer.

  1. Navigate to date/date/<packagename>/Databases/<database>.db
  2. To export, simply select the database and apply "Save as"
  3. To import, use the same previous procedure by choosing "Upload".

1


First, in your build.gradle (module:app) Add the following line:

compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

It’ll help us connect to the bank. Sync your Radle after this.

Then, in your project, right-click the folder app, click new > Folder > Assets Folder.

Inside the newly created folder, create another folder called "Databases". Paste the database you want to use inside that folder.

Now create a class called DatabaseOpenHelper.

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "meubd.db";
private static final int DATABASE_VERSION = 1;

public DatabaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

Now let’s instantiate and open the connection. Create the class DatabaseAccess;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;

/**
 * Construtor privado
 * @param context
 */
DatabaseAccess(Context context) {
    this.openHelper = new DatabaseOpenHelper(context);
}

/**
 * Retorna um singleton de DatabaseAccess
 *
 * @param context
 */
public static DatabaseAccess getInstance(Context context) {
    if (instance == null) {
        instance = new DatabaseAccess(context);
    }
    return instance;
}

/**
 * Abre a conexão
 */
public void open() {
    this.database = openHelper.getWritableDatabase();
}

/**
 * Fecha a conexão
 */
public void close() {
    if (database != null) {
        this.database.close();
    }
}

Create the methods you need within this class (a select, for example), open the connection in the class you are using DatabaseAccess db = new DatabaseAccess()

db.open();

Call your method:

db.MetododeSelect();

close the connection: db.close();

  • Guy doesn’t know how to thank, solved my problems :). I just altered some detail, but to suit my own tastes, but that worked perfectly, thank you very much !

0

There is this lib here that can help you, recently came out on Android Arsenal

https://android-arsenal.com/details/1/6154

0

Natively you cannot import a pre-loaded SQL database. The options are:

1) Do a base reverse to generate the DDL (script with the Inserts) and use it in your application’s Helper when creating the tables

2) Make an import using something like the tutorial you posted, which uses Filesystem to move the file to the correct location, or using third-party Libraries. Ex: https://github.com/jgilfelt/android-sqlite-asset-helper

NOTE: The tutorial you published seems to have been based on another tutorial you had read and had good acceptance on Stackoverflow in English. See if there’s any difference: https://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

Another more complex and interesting option would be to use Firebase and import the data from there at the first application run.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.