从应用程序导入和导出数据库

时间:2014-12-14 16:29:00

标签: database memory import android-studio export

我正在使用android studio和sqlite。每天早上我都需要从手机的内存中导入我的数据库,并每天下午导出到同一个文件夹。

目前我在资产中,但我需要每天充电。

任何帮助或手册都会有很大的帮助。

1 个答案:

答案 0 :(得分:0)

我将数据库应用程序复制到内存中。

public boolean Backup_bd(String Motivo,Context Contexto) {

    boolean Correcto = false;
    String message;

    try {
        boolean sdDisponible = false;
        boolean sdAccesoEscritura = false;

        //Comprobamos el estado de la memoria externa (tarjeta SD)
        String estado = Environment.getExternalStorageState();

        if (estado.equals(Environment.MEDIA_MOUNTED)) {
            sdDisponible = true;
            sdAccesoEscritura = true;
        } else if (estado.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
            sdDisponible = true;
            sdAccesoEscritura = false;
        } else {
            sdDisponible = false;
            sdAccesoEscritura = false;
        }

        //////////////////////////////////////////////////////////
        //Path aplication bd
        final String inFileName = "/data/data/your_proyect_name/databases/your_database_name";

        File dbFile = new File(inFileName);
        FileInputStream fis = new FileInputStream(dbFile);

        //
        String bdExportada = "/www/bd/your_database_name";

        String sCarpeta = Environment.getExternalStorageDirectory() + "/www";
        File Carpeta = new File(sCarpeta);

        if (Carpeta.exists()) {
        } else {
            Carpeta.mkdir();
        }

        String sCarpeta_bd = Environment.getExternalStorageDirectory() + "/www/bd";
        File Carpeta_bd = new File(sCarpeta_bd);

        if (Carpeta_bd.exists()) {
        } else {
            Carpeta_bd.mkdir();
        }

        String outFileName = Environment.getExternalStorageDirectory() + bdExportada;

        // Open the empty db as the output stream
        OutputStream output = new FileOutputStream(outFileName);

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

        // Close the streams
        output.flush();
        output.close();
        fis.close();

        //Toast.makeText(Contexto, "Copia realizada satisfactoriamente.", Toast.LENGTH_LONG).show();
        Correcto = true;

        /////////////////////////////////////////////////////////

    } catch (Exception ex) {
        message = ex.getMessage();
        Toast.makeText(Contexto, message, Toast.LENGTH_LONG).show();
    }

    return Correcto;
}