【问题标题】:Suddenly stop copying SQLite Database from assets突然停止从资产中复制 SQLite 数据库
【发布时间】:2021-06-18 07:48:06
【问题描述】:

我正在将 SQLite 数据库从 assets 文件夹复制到 android 应用程序。我正在使用下面提到的代码。它工作正常,我正在顺利地完成我的代码。突然,它停止将数据库复制到 Android 应用程序。

public class PreCreateDB {

    public static void copyDB (Context context) {
        try {
            String destPath = "/data/data/"+ context.getPackageName()+ "/databases";
            File f = new File(destPath);
            if (!f.exists()) {
                f.mkdir();
                rawCopy(context.getAssets().open("dbname.db"), new FileOutputStream(destPath + "/dbname.db"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void rawCopy(InputStream inputStream, OutputStream outputStream) throws IOException{

        byte[] buffer = new byte[1024];
        int length;
        while ((length=inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();

    }
}

我在设备文件资源管理器中找到了 db,我在其中看到了以下几行(不是整个表)。请帮忙

SQLite format 3   @                                                                   .�
   � �                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         W--ctableandroid_metadataandroid_metadataCREATE TABLE android_metadata (locale TEXT)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
   � �                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             en_IN

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    我将代码更改为以下代码,现在它可以工作了。

    public static void copyDB (Context context) throws IOException {
    
            try (SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/com.app.example/databases/dbname.db", null, 0)) {
                dbe.close();
            } catch (Exception e) {
                AssetManager am = context.getAssets();
                OutputStream os = new FileOutputStream("/data/data/com.app.example/databases/dbname.db");
                byte[] b = new byte[100];
                int r;
                InputStream is = am.open("dbname.db");
                while ((r = is.read(b)) != -1) {
                    os.write(b, 0, r);
                }
                is.close();
                os.close();
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多