代码之家  ›  专栏  ›  技术社区  ›  Mohammedsalim Shivani

Android环境下SQLite数据库的备份

  •  0
  • Mohammedsalim Shivani  · 技术社区  · 7 年前

    我在这里遵循了很多线程,但是他们建议将DB从一个位置复制到另一个位置,这不适合我,也不适合 Google Drive .

    任何指引都是宝贵的。

    2 回复  |  直到 7 年前
        1
  •  3
  •   astric mobiles    7 年前

    我用这种方法。

    public string DATABASE_NAME = "YOUR DATABASE NAME HERE";
    
       public void exportDB(){
            try {
                File sd = Environment.getExternalStorageDirectory();
                File data = Environment.getDataDirectory();
    
                if (sd.canWrite()) {
                    Log.d("TAG", "DatabaseHandler: can write in sd");
                    //Replace with YOUR_PACKAGE_NAME and YOUR_DB_NAME
                    String currentDBPath = "filepath here"+DATABASE_NAME;
                    //Replace with YOUR_FOLDER_PATH and TARGET_DB_NAME in the SD card
                    String copieDBPath = DATABASE_NAME;
                    File currentDB = new File(data, currentDBPath);
                    File copieDB = new File(sd, copieDBPath);
                    if (currentDB.exists()) {
                        Log.d("TAG", "DatabaseHandler: DB exist");
                        @SuppressWarnings("resource")
                        FileChannel src = new FileInputStream(currentDB).getChannel();
                        @SuppressWarnings("resource")
                        FileChannel dst = new FileOutputStream(copieDB).getChannel();
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                    }
                }
            } catch  (Exception e) {
                e.printStackTrace();
            }
    
        } 
    
        2
  •  0
  •   Mohammedsalim Shivani    7 年前

    最后,用这个 GitHub sample 我成功地实现了我需要的任何定制。现在是直播。

    推荐文章