zoukankan      html  css  js  c++  java
  • android sqlite导入数据

    @Override
        public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
     
            // TODO Auto-generated method stub
            try {
                InputStream in = mContext.getAssets().open("patch.sql");
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(in));
                String sqlUpdate = null;
                while ((sqlUpdate = bufferedReader.readLine()) != null) {
                    if (!TextUtils.isEmpty(sqlUpdate)) {
                        db.execSQL(sqlUpdate);
                    }
                }
                bufferedReader.close();
                in.close();
            } catch (SQLException e) {
                System.out.println(e.toString());
            } catch (IOException e) {
                System.out.println(e.toString());
            }   
        }
    

      导入脚本文件

    导入数据库文件

            dbfile="/data/data/app package name/databases/dbname.db";
        try {
                if (!(new File(dbfile).exists())) {
                    //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
                    InputStream is = this.context.getResources().openRawResource(
                            R.raw.sy8database); //欲导入的数据库
                    FileOutputStream fos = new FileOutputStream(dbfile);
                    byte[] buffer = new byte[BUFFER_SIZE];
                    int count = 0;
                    while ((count = is.read(buffer)) > 0) {
                        fos.write(buffer, 0, count);
                    }
                    fos.close();
                    is.close();
                }
    
                SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    

      

      sql脚本文件比生成的sqlite数据库文件要大很多,最好还是直接导入sqlite文件,可以压缩一空间

  • 相关阅读:
    [NoiPlus2016]换教室
    [HNOI2013]游走
    [Noi2002]Savage
    [SDOI2010]古代猪文
    [JSOI2008]最小生成树计数
    [SCOI2010] 连续攻击游戏
    文艺平衡树
    指针FHQTreap
    HAOI2007 上升序列
    HNOI2008 玩具装箱
  • 原文地址:https://www.cnblogs.com/jiezzy/p/5091260.html
Copyright © 2011-2022 走看看