0

My App will contain a big database, so how do I insert the data ? I'd like to do that on the PC and then put it in the app, since its a lot of data.

I've seen the posts insert initial data to sql lite android / adding your own SQLite database to an android application / Ship an application with a database But I don't understand or im not sure its the best way.

10
  • I marked this question as close/duplicate, because there is no information regarding why this solution is not suitable in your case, or how you failed to implement it. Commented Jan 27, 2014 at 17:27
  • Guys, you are sending me to a solution of the 2009, and deny the community to find a new one. Honestly I dont like that solution and I don't like commonware solution too. Unlock this question please. Commented Jan 27, 2014 at 22:09
  • the question was closed because there is nothing in your question that suggest that another answer would be more suitable. you should try to explain what you have tried, why this solution does not work / is not suited for your solution. Just so you know, I have used this solution for several applications myself and still do. Commented Jan 28, 2014 at 14:23
  • What I dont like is: 1 the db duplication will use double of the space. 2: it use custom function, and the public functions oncreate and on upgrade are empty, and you must use those createdatabase() and opendatabase()(this one that im not sure what it does, and why it says openreadonly), and where ? oncreate of the main activity? Commented Jan 28, 2014 at 15:06
  • and then what ? do i have to use getredabledatabase()or writable? and then can I use execSQL/ rawQuery ? Commented Jan 28, 2014 at 15:09

2 Answers 2

1

You can put the database with all the data you need in your resources and then initiate the application with this values.

  1. First you need to create a folder "assets" on your project
  2. Create your own Database Helper (extend SQLiteOpenHelper)
  3. Then you will implement a "copy" method, something like that:

    private void copyDatabase(String dbname,Context context) throws IOException {

        InputStream is = context.getAssets().open(dbname);
        String outFileName = DB_PATH + dbname;
        OutputStream out = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        is.close();
        out.flush();
        out.close();
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

If you have a lot of data, the option to ship prepared database is the best one. It will take the less time on the first start-up to launch. It's the simplest one, after all.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.