1

I am trying to use SQLiteAssetHelper and it requires me to create database on PC and copy it to assets/databases/databasename.db. I do that, but when I check on the device, nor the database, nor the databases folder is there under my app's data folder.

I did this based on a video, and the guy there doesn't do anything else to make the copy work.

So, are the files supposed to be automatically copied over to device? If not, how can I copy the files?

Edric
  • 24,639
  • 13
  • 81
  • 91
KijeviGombooc
  • 304
  • 2
  • 13

2 Answers2

1

So, are the files supposed to be automatically copied over to device? If not, how can I copy the files?

Yes it will BUT only when an attempt is made to access the database. Just instantiating the class that extends SQLiteAssetHelper will not copy the database and thus if that is all you do then the database will not be visible.

The steps when using SQliteAssetHelper to take should be :-

  1. Create your project.
  2. Amend the build.gradle to include implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' in the dependencies section.
  3. Create the assets folder and then the databases folder in the assets folder and
  4. Copy the database file into the databases folder.
  5. Create the Database Helper class that extends SQliteAssetHelper.
  6. Instantiate the Database Helper
  7. Access the database (this is when the database is copied).

If when doing step 5 you use, as an example (based upon the information in the question) :-

public class DatabaseHelper extends SQLiteAssetHelper {

    public static final String DATABASENAME = "databasename.db"; //<<<<<<<<< MUST match file name in assets/databases
    public static final int DATABASEVERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASENAME, null, DATABASEVERSION);
        this.getWritableDatabase(); //<<<<< will force database access and thus copy database from assets
    }
}

Then when doing 6 (instantiating the database helper), then 7 is done because this.getWritableDatabase(); accesses (implicitly opens) the database.

e.g.

public class MainActivity extends AppCompatActivity {

    DatabaseHelper mDBHelper; //<<<<< declares the database helper

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHelper = new DatabaseHelper(this); // instantiates the database helper
    }
}
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Thanks! I did everything like you described. Your answer is perfect, however, my problem wasn't what I had thought. I just simply accidently had null pointer passed in SQLiteAssetHelper constructor. – KijeviGombooc Dec 15 '19 at 21:31
0

The procedure you're explaining requires root access to the app storage on the device which is impossible on a normal non-rooted device due to security considerations. For this, you need a rooted device OR you could use emulator in Android Studio, that's much easier and user friendly approach:

Open emulator from Android Studio -> Settings -> Memory -> Internal Storage -> Others

A pop-up window will open. Click explore. You will get access to your app's internal storage so you can manage the database folder as well.

P.S.: Also, you might just package your app with the database table you have, just as with any other file, simply place it under your src/main/assets folder and access from code in runtime. Code samples are available in this thread : Android - Copy assets to internal storage (suggested by @Ezaldeen sahb)

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
  • That was what I was trying to explain, I did put the database file under src/main/assets. The library I use, SQLiteAssetHelper, is supposed to find the file under data/data/my.package.name/databases/mydatabase.db The thing is, the file isn't there. I use emulator, and I view this in the Tools->View Windows->Device file explorer. The database folder is just simply not there – KijeviGombooc Dec 15 '19 at 11:55