【问题标题】:create internal db file android sqlite创建内部数据库文件android sqlite
【发布时间】:2015-08-14 07:24:30
【问题描述】:

当数据提交到数据库时,我正在尝试创建内部 db 文件。当用户在文本字段中输入值并单击按钮时,'SAMPLE.db' db 文件应该在内部存储的 'MYTEST' 文件中创建。这是我的代码。但它不起作用。

-主Activity oncreate-

btn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    String Id=mEDitTxt.getText().toString();
                    String Unm=mEDitTxt2.getText().toString();


                    insertconfig(Id, Unm);
                    DbBackup.copyDatabase(getApplicationContext());

                }
            });

-数据库备份-

public class DbBackup {

    public static void copyDatabase(Context c) {

        String DATABASE_NAME = "SAMPLE.db";

        String databasePath = c.getDatabasePath(DATABASE_NAME).getPath();
        File f = new File(databasePath);
        OutputStream myOutput = null;
        OutputStream myOutputInternal = null;
        InputStream myInput = null;
        Log.d("testing", " lots db path " + databasePath);
        Log.d("testing", " lots db exist " + f.exists());

        if (f.exists()) {
            try {


                File directoryIntenal = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MYTEST/");



                if (!directoryIntenal.exists())
                    directoryIntenal.mkdir();

                myOutputInternal = new FileOutputStream(directoryIntenal.getAbsolutePath()
                        + "/" + DATABASE_NAME);

                myInput = new FileInputStream(databasePath);


                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0) {

                    myOutputInternal.write(buffer, 0, length);
                }


                myOutputInternal.flush();

            } catch (Exception e) {
            } finally {
                try {

                    if (myOutputInternal != null) {
                        myOutputInternal.close();
                        myOutputInternal = null;
                    }
                    if (myInput != null) {
                        myInput.close();
                        myInput = null;
                    }
                } catch (Exception e) {
                }
            }
        }
    }
}

【问题讨论】:

  • 显示 Logcat 错误。如果没有显示 change catch 语句:catch (Exception e) { Log.e(TAG, "Error", e) }

标签: android database file android-studio storage


【解决方案1】:

当您想要导出或导入您的数据库时调用此方法,

public class DbExportImport {

    public static final String TAG = DbExportImport.class.getName();

    /**
     * Directory that files are to be read from and written to *
     */

    public static final String PACKAGE_NAME = "com.vcs.androiddatabaseexample";
    public static final String DATABASE_NAME = "crud.db";


    public static final File DATA_DIRECTORY_DATABASE = new File(
            Environment.getDataDirectory() + "/data/" + PACKAGE_NAME
                    + "/databases/" + DATABASE_NAME);

    public static final String DATA_DIRECTORY_DATABASE_PATH =
            Environment.getDataDirectory() + "/data/" + PACKAGE_NAME
                    + "/databases/" + DATABASE_NAME;

    public static boolean exportSyncDbNew(String Filename, String FolderPath) {
        if (!SdIsPresent())
            return false;

        File dbFile = DATA_DIRECTORY_DATABASE;
        if (dbFile.exists()) {
            // String filename = Filename+"_SAT.db";
            File exportDir = new File(FolderPath);
            if (!exportDir.exists()) {
                exportDir.mkdirs();
            }
            File file = new File(exportDir, Filename);
            Utils.printLoge(5, "#### exportDir", "-->" + FolderPath);
            Utils.printLoge(5, "#### Filename", "-->" + Filename);
            Utils.printLoge(5, "DATA_DIRECTORY_DATABASE", "-->" + DATA_DIRECTORY_DATABASE);
            try {
                if (!file.exists()) {
                    file.createNewFile();
                    copyFile(dbFile, file);
                    return true;
                } else {
                    return false;
                }
            } catch (IOException e) {
                //e.printStackTrace();
                Constant.isFielCopeError = true;
                Constant.fileCopeException = "Upload: " + Utils.ConvertExceptionToString(e);
                return false;
            }
        } else {
            Constant.isFielCopeError = true;
            Constant.fileCopeException = "Upload: DB not available";
            return false;
        }
    }

    public static boolean restoreDownloadedDb(String FileName) {
        if (!SdIsPresent())
            return false;

        File exportFile = DATA_DIRECTORY_DATABASE;
        //File importFile = IMPORT_Downloaded_FILE;
        File importFile = new File(Constant.ECTERNAL_STORAGE_APP_USER1, FileName);


        if (exportFile.exists()) {
            exportFile.delete();
//            Log.e(TAG, "exportFile delete");
        }

        if (!importFile.exists()) {
//            Log.d(TAG, "File does not exist");
            return false;
        }

        try {
            exportFile.createNewFile();
            //copyFile(importFile, exportFile);
            copyDataBase(importFile, exportFile);
            return true;
        } catch (IOException e) {
            Utils.printLoge(5, "restoreDownloadedDb", "error-->" + e.getMessage().toString());
            e.printStackTrace();
            return false;
        }
    }

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 1970-01-01
    • 2023-04-03
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多