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文件,可以压缩一空间

你可能感兴趣的:(android sqlite导入数据)