Loading an externally created SQLite database

0

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

Now I have a SQLite database with considerable records and I need to use it in my application, but I can not do that. I looked this tutorial , but I did not succeed. When running the application, I always fired an exception with "Could not copy file".

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

    
asked by anonymous 05.09.2017 / 13:27

3 answers

1

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

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

It will help us connect with the bank. Synchronize your gradient after that.

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

Within the newly created folder, create another folder called "databases". Paste the bank you want to use inside this 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 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();

    
05.09.2017 / 16:25
0

Natively can not import a pre-loaded SQL database. The options are:

1) Reverse the base to generate the DDL (script with the inserts) and use it in the Helper of your application 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: link

NOTE: The tutorial you posted seems to have been based on another tutorial that you had already read and was well accepted at StackOverFlow in English . See if you have any differences: link

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

    
05.09.2017 / 15:57
0

There's this lib here that can help you out recently on Android Arsenal

https://android-arsenal.com/details/1/6154
    
06.09.2017 / 01:56