0

I would like to import a database which is larger than 10 MB. I have not found a solution on the internet... Furthermore, I would like insert this DB in the final APK. Do you have any ideas ?

Sorry for my English. I am French

Ron
  • 24,175
  • 8
  • 56
  • 97
user1360503
  • 19
  • 1
  • 5

3 Answers3

1

Basically what I did once for similar database was the thing described here. You create the database and put it in your assets folder. Then have SQLiteOpenHelper that copies it in the place where the application databases should reside (i.e. the private application storage).

However with as big file I needed to split the database file in several files as described here, because files larger than 1MB can not be read from the assets folder. However you should be aware that your apk will become significantly large, of course.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • OK I have to split the file. db However, the link does not say how to split the file ... How to do that? and merge the files? As in the link? – user1360503 Apr 27 '12 at 07:52
  • @user1360503 There is one comment over there: `on linux and mac there's a command line tool called split` for me this worked, because I was on Linux. Also the merging is simple: moving the file is done with `InputStream`s either way. Just open the output file with append flag. – Boris Strandjev Apr 27 '12 at 07:55
0

I think you would either have to:

  1. Hardcode the insert statement into the application so when the application installs it performs this function, or
  2. Host the DB on the web and have your app pull the data into its DB.
  3. Host the DB on the web and have your app fetch only the data it requires, you can then have it save into the application database as it does this (I think this is the best solution of my solutions)
ASceresini
  • 419
  • 3
  • 7
  • Thanks but the problem Is That i must have the entire DB in the application. If I host the DB, it would require me to download all the DB and the insertions in the application takes an enormous amount of time ... – user1360503 Apr 27 '12 at 07:45
  • What I am saying in option three, is that you host it, and then you build your app to pull only what it needs and store it as it is pulled. However, it sounds like you want to create the DB when the user first installs the app, so from my experience you would need to build the DB at time of install. Therefore, an insert statement containing all the data (REALLY BIG STATEMENT) would need to be hardcoded into your application – ASceresini Apr 27 '12 at 07:46
  • Indeed, if this is the only solution, which will allow to keep everything in the application, I would be forced to hardcoded instruction – user1360503 Apr 27 '12 at 07:56
-1

can use ZipInputStream to handle the large files.use zip util zip your files in project's asset folder

InputStream dataSource = getAssets().open("assets.zip");
unzip(dataSource, MainTab1Activity.defaultDBPath);

public static void unzip(InputStream zipFileName, String outputDirectory) {
        try {
            ZipInputStream in = new ZipInputStream(zipFileName);
            // 获取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry,
            // 当getNextEntry方法的返回值为null,则代表ZipInputStream中没有下一个ZipEntry,
            // 输入流读取完成;
            ZipEntry entry = in.getNextEntry();
            while (entry != null) {

                // 创建以zip包文件名为目录名的根目录
                File file = new File(outputDirectory);
                file.mkdir();
                System.out.println("file.mkdir()");
                if (entry.isDirectory()) {
                    String name = entry.getName();
                    name = name.substring(0, name.length() - 1);

                    file = new File(outputDirectory + File.separator + name);
                    file.mkdir();

                } else {
                    file = new File(outputDirectory + File.separator + entry.getName());
                    file.createNewFile();

                    FileOutputStream resultFileOutput = new FileOutputStream(file);
                    byte[] buf = new byte[1024 * 1024];// 设定缓冲区大小
                    int bsize = 0;
                    while ((bsize = in.read(buf)) != -1) {
                        resultFileOutput.write(buf, 0, bsize);
                    }
                    //in.close();
                    resultFileOutput.close();
                    System.gc();


//                    FileOutputStream out = new FileOutputStream(file);
//                    int b;
//                    while ((b = in.read()) != -1) {
//                        out.write(b);
//                    }
//                    out.close();
                }
                // 读取下一个ZipEntry
                entry = in.getNextEntry();
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
    }
joke
  • 9
  • 6