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.

Community
  • 1
  • 1
Jacks23465
  • 21
  • 2
  • 4
  • 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. – njzk2 Jan 27 '14 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. – Jacks23465 Jan 27 '14 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. – njzk2 Jan 28 '14 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? – Jacks23465 Jan 28 '14 at 15:06
  • and then what ? do i have to use getredabledatabase()or writable? and then can I use execSQL/ rawQuery ? – Jacks23465 Jan 28 '14 at 15:09
  • 1/ yes, in your application and after extraction. there is no way around this, unless you download the db from a server. 2/ oncreate and onupgrade are empty because you don't let android create and upgrade the database for you since you imported it in the first place. I don't understand your other questions, once the database is in place, you use like like any other database. `readable` if you need to read it, `writable` to write in it... It behaves like any other database. There is no difference apart from the creation/copy part. – njzk2 Jan 28 '14 at 15:13
  • to elaborate on 1/ the apk is not editable. anything you put in your apk can never be modified. Access to this data is particular due to the nature of the apk file. In order to fully access a database that you would have integrated in your apk, you need to copy it outside, hence the size duplication. – njzk2 Jan 28 '14 at 15:15
  • to elaborate on 2/ onCreate and onUpgrade are callbacks used by android to notify you that you have created a db file and need to put somthing in it or that you have changed the version number of your db and may want to upgrade the tables. In the case of a shipped database, there is no point in having those callbacks, as the structure in the initial db is usually fixed. onUpgrade can be used in some cases, though. (Both these points could have been the object of an actual question, possibly with several answers, had they been actually asked) – njzk2 Jan 28 '14 at 15:19
  • OR query() ? I have the feeling thats its"out of the standard", well maybe there is not a standard... but I'D LIKE TO BE ABLE TO BE ABLE TO DISCUSS about this! – Jacks23465 Jan 28 '14 at 15:25
  • ok, thanks for your help njzk2! ur kind! one more question, at the and of the article he says, "call the createDataBase() and openDataBase() methods" , when ? oncreate of the activity ? or each time i need the db ? – Jacks23465 Jan 28 '14 at 15:40

2 Answers2

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.

Community
  • 1
  • 1
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
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();
    }
    
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18