【问题标题】:Import and Export database from application从应用程序导入和导出数据库
【发布时间】:2014-12-14 16:29:00
【问题描述】:

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

目前我放置在Assets中,但我需要每天充电。

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

【问题讨论】:

  • 您到底在寻找什么?在哪里放置数据库转储?请澄清您的问题。
  • 我的 android 应用程序是对桌面应用程序的补充,这些天从我的桌面应用程序创建一个新的 sqlite 数据库,我需要将我的手机连接到 USB 端口并将数据库加载到我的应用程序。
  • Currently I place in Assets 然后将数据库放在 SD 卡上的某个位置。并将其从/复制到那里而不是从资产中复制。

标签: database memory import android-studio export


【解决方案1】:

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

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多