3

I have an existing database with some data (something like a dictionary) and I want to put it in installation apk-package, I'll explain - I just want to use my existing database when the app is installed. Which is the best way to do it?

  1. Put a database in res/raw/ and then copy it on the SD card when the app is first launched.

  2. Create a SQL script which will create a database structure and fill the data and execute it in a db-helper class. I think it is a very bad idea, because I have about 1 million records from all tables and it will be hard to work with that script.

  3. Use a static non-writable database like in this post

Anything else?

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
whizzzkey
  • 926
  • 3
  • 21
  • 52

3 Answers3

2

I would suggest you use the SQLiteAssetHelper library (https://github.com/jgilfelt/android-sqlite-asset-helper). It's an Android helper class to manage database creation and version management using an application's raw asset files.

This class provides developers with a simple way to ship their Android app with an already existing SQLite database (which may be pre-populated with data) and to manage it's initial creation and any upgrades required with subsequent version releases.

Your preloaded SQLite database will be stored in a zipped file in the assets folder and the SQLiteAssetHelper .jar library will be stored in your lib folder, make sure that you add it into your build path.

Then you can create a class with the folllowing:

A sample class that loads a pre-loaded database of title of songs and its title:

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DatabaseManager {
    //  DECLARATION OF ALL THE VARIABLES AND CONSTANT THAT WILL BE USED TO CREATE THE TABLE

    private static final String DATABASE_NAME = "SongDatabase"; 
    private static final String DATABASE_TABLE = "Song";

    //  DECLARATION OF ALL THE COLUMN REQUIRED TO BE CREATED

    public static final String KEY_ROWID = "_id";
    public static final String KEY_AUTHOR = "author";
    public static final String KEY_TITLE = "title";

    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase ourDatabase;
    private final Context ourContext;

    public class DatabaseHelper extends SQLiteAssetHelper {     
        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);  
        }
    }

    public DatabaseManager(Context context){
        ourContext = context;       
    }

    //  open the database for access
    public DatabaseManager open() throws SQLException {
        mDbHelper = new DatabaseHelper(ourContext);
        ourDatabase = mDbHelper.getWritableDatabase();
        return this;
    }

    //  Enter Values into the database or create database values
    public long createRecords(String author, String title) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_AUTHOR, author);
        initialValues.put(KEY_TITLE, title);
        return ourDatabase.insert(DATABASE_TABLE, null, initialValues); 
    }

    // close the database after creating the values for security purposes
    public void close() {
        mDbHelper.close();
    }
}
xarlymg89
  • 2,552
  • 2
  • 27
  • 41
Ibukun Muyide
  • 1,294
  • 1
  • 15
  • 23
1

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

with this you can store data in your assets folder and copy it to you apk database the problem is there is a limit to file size ie 10MB and you can not delete the data cos asset folder is read only so you will have duplicates and increase the apk size

https://github.com/jgilfelt/android-sqlite-asset-helper

the other option is you use it directly good luck :)

Vikas Rathod
  • 374
  • 2
  • 14
0

@Whizzzkey at your request here is the answer :)

put the database in the assets folder, though you may not perform write operations from it, and for that you would have to copy the db to the sdcard (which is expensive) or to memory.

Happy coding.

xarlymg89
  • 2,552
  • 2
  • 27
  • 41