本文介绍了没有空LiteSQL DB在启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是相当简单的问题。我已经在Android的东西太年轻了。我想美元,其中将使用数据库p $ ppare应用。在我展示每一个例子,有一个空数据库应用程序的地方首先开始,后面还有一些插件。我想有相当大分贝的应用程序,所以我想应用程序启动时,以填补分贝。我怎样才能prepare分贝并将其安装到程序?


解决方案

  1. 把你的数据库填补在包的资产目录,


  2. 在应用程序运行只需要复制该数据库到应用程序一样的内部存储空间
    数据/数据​​/<包名称> /数据库目录


  3. 然后使用它。

编辑:这从资产目录拷贝数据库数据库目录,

 私人无效copyDataBase()抛出IOException        尝试{
            //打开本地数据库的输入流
            InputStream的myInput = myContext.getAssets()开(你的数据库文件名)。            //路径刚刚创建的空分贝
            字符串outFileName =/数据/数据​​/<包名称> /数据库/;
            的OutputStream myOutput =新的FileOutputStream(outFileName);            //传递从inputfile中字节到OUTPUTFILE
            字节[]缓冲区=新的字节[1024];
            INT长;
            而((长度= myInput.read(缓冲液))大于0){
                myOutput.write(缓冲液,0,长度);
            }            //关闭流
            myOutput.flush();
            myOutput.close();
            myInput.close();        }赶上(例外五){            Log.e(错误,e.toString());        }    }

I think that this is rather easy question. I am too young in android stuff already. I want to prepare application which will be using database. In every example I've shown, there is an empty database where application is firstly started and after that there are some inserts. I want to have app with rather big db so I want to have filled db when app is started. How can I prepare db and attach it to program?

解决方案
  1. put your filled database in Package's Asset directory,

  2. at application runtime just copy that database to application's internal storage likedata/data/<package name>/database directory.

  3. then use it.

EDIT: this for copy database from asset directory to database directory,

private void copyDataBase() throws IOException {

        try {
            // Open your local db as the input stream
            InputStream myInput = myContext.getAssets().open("your Database file name");

            // Path to the just created empty db
            String outFileName = "/data/data/<package name>/databases/";
            OutputStream myOutput = new FileOutputStream(outFileName);

            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

        } catch (Exception e) {

            Log.e("error", e.toString());

        }

    }

这篇关于没有空LiteSQL DB在启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 19:56