0

How is it that some dictionaries such as merriam dictionary (Offline dictionary) when the application was installed , the words are there instantly, and time is not required to insert a list of words and definition into the database? I am a beginner and is currently developing an android application that consist of about 30K words and it will take around 15+ minutes for it to insert all the data into the database before the user can search for that particular data. And I am looking for a method that can fix this. Could someone please tell me a way to do it ?

Thank you

AuroraBlaze
  • 421
  • 2
  • 8
  • 25
  • "Take around 15+ minutes" - are you reading from a file and creating the DB that way? Faster might be to just ship with an sqlite db in /assets/ and copy it to data dir for instant use. See here: http://stackoverflow.com/questions/6198528/how-to-create-sqlite-database-at-the-time-of-android-app-installation – Ken Wolf Apr 11 '14 at 15:29

2 Answers2

1

My guess is that these apps are using an already SQLite database with all the data they need already populated. You can import populated databases to your app with something like this :

public class DataBaseAdapter {

String DB_NAME = "DBNAME.db";
String DIR = "/data/data/packageName/databases/";
String DB_PATH = DIR + DB_NAME;

private DataBaseHelper mDbHelper;
private SQLiteDatabase db;
private Context context;

public DataBaseAdapter(Context context) {
    this.context = context;
    mDbHelper = new DataBaseHelper(this.context);
}

class DataBaseHelper extends SQLiteOpenHelper {
    private boolean createDatabase = false;
    @SuppressWarnings("unused")
    private boolean upgradeDatabase = false;
    Context context;

    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.context = context;
    }

    public void initializeDataBase() {

        getWritableDatabase();

        if (createDatabase) {
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }

    }

    private void copyDataBase() throws IOException {
        InputStream input = context.getAssets().open(DB_NAME);
        OutputStream output = new FileOutputStream(DB_PATH);

        byte[] buffer = new byte[1024];
        int length;

        try {
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        }

        finally {
            try {
                if (output != null) {
                    try {
                        output.flush();
                    } finally {
                        output.close();
                    }
                }
            } finally {
                if (input != null) {
                    input.close();
                }
            }
        }

        getWritableDatabase().close();
    }

    public void onCreate(SQLiteDatabase db) {

        createDatabase = true;
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        upgradeDatabase = true;

    }

    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
    }
}

public DataBaseAdapter open() {
    mDbHelper.initializeDataBase();
    if (db == null)
        db = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    db.close();
}

}

you can then add methods to get data from database and this class can be used in your activity by calling open then the method to get data then close.

  • umm may i know what should the "import populated databases" consist of ? should it contain a list of insert statements? – AuroraBlaze Apr 11 '14 at 15:53
  • I had make research on this section but then i found out that I have to get the populated database , how to i get the populated database/extract it to be added into the application? – AuroraBlaze Apr 12 '14 at 10:28
0

Your application should include a pre-populated database for offline access with it's install. That will avoid each user having to run the INSERT step on their device.

Is there a particular reason you need to run the INSERTS post-install?

tenfishsticks
  • 452
  • 5
  • 13
  • I would like to make it similar to the merrian dictionary but I am not sure of how to implement it, so I am using the Insert into for the time being as I have a list of words in a txt file where it will first separate the word into 2 parts before inserting it into the database, but it takes about 15 minutes + for it load everything. May I know what should the pre-populated database consist of ? Are there any tutorials that I can refer to ? thank you – AuroraBlaze Apr 11 '14 at 16:08