English 中文(简体)
数据库备份和机器人
原标题:Database backup in android

我在应用程序中使用Sql数据库, 我想获取数据库的备份。 我存有以下疑问:

1.I am running the application in emulator,for checking whether i have to plug some external storage to check ,if not in my system how can i check. 2.I am using the following code in my application,in that sdcard.write option is showing false,what wrong in this.

福林是我的代码:

  try {
                    File sd = Environment.getExternalStorageDirectory();
                    File data = Environment.getDataDirectory();

                    java.lang.System.out.println("data="+sd.getAbsolutePath());
 java.lang.System.out.println("data="+sd.canWrite());--->Showing as false

                    if (sd.canWrite()) {
                        String currentDBPath = "\data\com.budget\databases\budget";
                        String backupDBPath = "budget";
                        File currentDB = new File(data, currentDBPath);
                        File backupDB = new File(sd, backupDBPath);

                        java.lang.System.out.println("backup="+backupDB.getAbsolutePath());

                        if (currentDB.exists()) {
                            FileChannel src = new FileInputStream(currentDB).getChannel();
                            FileChannel dst = new FileOutputStream(backupDB).getChannel();
                            dst.transferFrom(src, 0, src.size());
                            src.close();
                            dst.close();
                        }
                    }
                } catch (Exception e) {
                }
问题回答

Data Backup Documentation in android. If your Android application has a database and you want your users to be able to backup the database and restore it as they see fit, you’ll need to mess about with database files.

下面是一个名为 DbExportimport 的类。 一旦您设置了正确的值, 您需要做的就是从您的应用程序中调用导出Db ()、 导入Db () 或恢复Db () 来执行必要的操作 。

在更改您的软件包名称或应用程序签名密钥时,此选项也是有用的临时措施,因为您的应用程序将被新安装,您将失去数据库 。 (re )。

但是,如果您需要的话, 您可能想要直接扩展备份 Agency : * 在数据库中备份数据。 如果您有一个 SQLite 数据库, 当用户重新安装您的应用程序时, 您想要恢复该数据库, 您需要建立一个自定义备份Agency, 在备份操作中读取适当的数据, 然后创建您的表格, 在恢复操作中插入数据 ( https:// stackoverflow.com/ questions/52826/ 和roid- backup- restore- how- to- backup- a- international- database" > More )。

现在您也可以从 sdcard < a href="\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

这是另一个解决方案,就像你的问题

If you are using Eclipse, go to Window - Android Virtual Device Manager, select the AVD you are using and click Edit, in the Hardware options select New and then select the SD Card Support. Then just define the storage size and click Edit AVD, to save the changes. To check if sd is mounted use:

if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
    //code for sd mounted
}else{
    //code for sd not mounted
}

检查您是否在列表中设置了权限 。

<manifest ...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

文件扩展名 (.db ) 在上述节选中缺失 :

currentDBPath = "\data\com.budget\databases\udget.db";




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签