尝试在 Android 中复制 SQLite 数据库并获取 "No such file or directory"
Trying to copy SQLite database in Android and getting "No such file or directory"
我有一个数据库,我想将它复制到 SD 卡中进行备份,我找到了这段代码:
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ "com.example.myapp" +"/databases/"+"mydatabase";
String backupDBPath = "/storage/extSdCard/mydatabase";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
//e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
}
正如我在标题中所写的那样,当我尝试这样做时出现 No such file or directory
ENOENT 错误。
显然数据库应该存储在那里。
我已经尝试将路径更改为一些不同的东西,但仍然没有......
我试过了:
"/data/"+ "com.example.myapp" +"/"+"mydatabase.db"
和
"//data//"+ "com.example.myapp" +"//databases//"+"mydatabase"
Edit1:也试过这个但还是不行:
String currentDBPath = this.getDatabasePath("mydatabase").toString();
等没有任何效果,我也找不到以任何方式检查路径的方法。
我基本上已经创建了数据库,向其中插入了几行并具有一些基本功能,例如 add()
和 delete()
等。我是否遗漏了什么?我不知道我还能尝试什么。
以下是 logcat 对此的看法:
01-18 11:34:46.215 10337-10337/com.example.myapp D/DBPATH: Database path is /data/data/com.example.myapp/databases/mydatabase.db
01-18 11:34:46.225 10337-10337/com.example.myapp W/System.err: java.io.FileNotFoundException: /data/data/com.example.myapp/databases/mydatabase.db: open failed: ENOENT (No such file or directory)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at java.io.FileInputStream.(FileInputStream.java:76)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at com.example.myapp.TabsActivity.exportDB(TabsActivity.java:183)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at com.example.myapp.TabsActivity.onOptionsItemSelected(TabsActivity.java:119)
如下所示,问题可能与数据库无关,而可能与外部存储有关,因为目录不存在。解决方法是创建目录。希望以下内容会起作用(对我有用):-
try {
source = new FileInputStream(currentDB).getChannel();
(new File(backupDB.getParent())).mkdirs(); //<<<<<<<<<<<<<< ADDED
destination = new FileOutputStream(backupDB).getChannel();
//destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
- 为了测试,我注释掉了 t运行sfer. P.S。 运行 可以接受 运行sfer.
- 也可能存在权限问题(我的测试是在 genymotion 模拟器上进行的)。
- 刚在真实设备上测试过,如果没有权限也会出现同样的错误运行
- 刚刚在真实设备上测试了 Manifest 中的设置权限以及版本 > 23 的代码。复制然后工作。 :-
这可能有用
获取您的代码并根据(检查路径,查看输出)进行调整:-
java.io.FileNotFoundException:
/mnt/sdcard/storage/extSdCard/mydatabase: open failed: ENOENT (No such
file or directory)
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ getPackageName() +"/databases/"+DatabaseHandler.DATABASE_NAME;
String retreivedDBPAth = getDatabasePath(DatabaseHandler.DATABASE_NAME).getPath();
String backupDBPath = "/storage/extSdCard/mydatabase";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
File retrievedDB = new File(retreivedDBPAth);
Log.d("PATHS", " CurrentDB=" +
currentDBPath + "\n\t" + currentDB.getPath() +
"\n\tExists=" + String.valueOf(currentDB.exists()) +
"\nBackup=" + backupDBPath + "\n\t" + backupDB.getPath() +
"\n\tExists=" + String.valueOf(backupDB.exists()) +
"\nRetrieved DB=" + retreivedDBPAth + "\n\t" + retrievedDB.getPath() +
"\n\tExists=" + String.valueOf(retrievedDB.exists())
);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
//destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
}
ENOENT 不是针对数据库,而是根据 的外部存储:-
01-18 11:11:32.017 2689-2689/? D/PATHS: CurrentDB=/data/example.com.so48304909_spinner/databases/spinnerExample
/data/data/example.com.so48304909_spinner/databases/spinnerExample
Exists=true
Backup=/storage/extSdCard/mydatabase
/mnt/sdcard/storage/extSdCard/mydatabase
Exists=false
Retrieved DB=/data/data/example.com.so48304909_spinner/databases/spinnerExample
/data/data/example.com.so48304909_spinner/databases/spinnerExample
Exists=true
01-18 11:11:32.021 2689-2689/? W/System.err: java.io.FileNotFoundException: /mnt/sdcard/storage/extSdCard/mydatabase: open failed: ENOENT (No such file or directory)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:416)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-18 11:11:32.021 2689-2689/? W/System.err: at example.com.so48304909_spinner.MainActivity.exportDB(MainActivity.java:162)
01-18 11:11:32.021 2689-2689/? W/System.err: at example.com.so48304909_spinner.MainActivity.onCreate(MainActivity.java:38)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.Activity.performCreate(Activity.java:5008)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.access0(ActivityThread.java:130)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.os.Looper.loop(Looper.java:137)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
01-18 11:11:32.021 2689-2689/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-18 11:11:32.021 2689-2689/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-18 11:11:32.021 2689-2689/? W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.Posix.open(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:400)
01-18 11:11:32.021 2689-2689/? W/System.err: ... 18 more
我有一个数据库,我想将它复制到 SD 卡中进行备份,我找到了这段代码:
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ "com.example.myapp" +"/databases/"+"mydatabase";
String backupDBPath = "/storage/extSdCard/mydatabase";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
//e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
}
正如我在标题中所写的那样,当我尝试这样做时出现 No such file or directory
ENOENT 错误。
显然数据库应该存储在那里。 我已经尝试将路径更改为一些不同的东西,但仍然没有...... 我试过了:
"/data/"+ "com.example.myapp" +"/"+"mydatabase.db"
和
"//data//"+ "com.example.myapp" +"//databases//"+"mydatabase"
Edit1:也试过这个但还是不行:
String currentDBPath = this.getDatabasePath("mydatabase").toString();
等没有任何效果,我也找不到以任何方式检查路径的方法。
我基本上已经创建了数据库,向其中插入了几行并具有一些基本功能,例如 add()
和 delete()
等。我是否遗漏了什么?我不知道我还能尝试什么。
以下是 logcat 对此的看法:
01-18 11:34:46.215 10337-10337/com.example.myapp D/DBPATH: Database path is /data/data/com.example.myapp/databases/mydatabase.db
01-18 11:34:46.225 10337-10337/com.example.myapp W/System.err: java.io.FileNotFoundException: /data/data/com.example.myapp/databases/mydatabase.db: open failed: ENOENT (No such file or directory)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at java.io.FileInputStream.(FileInputStream.java:76)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at com.example.myapp.TabsActivity.exportDB(TabsActivity.java:183)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at com.example.myapp.TabsActivity.onOptionsItemSelected(TabsActivity.java:119)
如下所示,问题可能与数据库无关,而可能与外部存储有关,因为目录不存在。解决方法是创建目录。希望以下内容会起作用(对我有用):-
try {
source = new FileInputStream(currentDB).getChannel();
(new File(backupDB.getParent())).mkdirs(); //<<<<<<<<<<<<<< ADDED
destination = new FileOutputStream(backupDB).getChannel();
//destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
- 为了测试,我注释掉了 t运行sfer. P.S。 运行 可以接受 运行sfer.
- 也可能存在权限问题(我的测试是在 genymotion 模拟器上进行的)。
- 刚在真实设备上测试过,如果没有权限也会出现同样的错误运行
- 刚刚在真实设备上测试了 Manifest 中的设置权限以及版本 > 23 的代码。复制然后工作。 :-
这可能有用
获取您的代码并根据(检查路径,查看输出)进行调整:-
java.io.FileNotFoundException: /mnt/sdcard/storage/extSdCard/mydatabase: open failed: ENOENT (No such file or directory)
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ getPackageName() +"/databases/"+DatabaseHandler.DATABASE_NAME;
String retreivedDBPAth = getDatabasePath(DatabaseHandler.DATABASE_NAME).getPath();
String backupDBPath = "/storage/extSdCard/mydatabase";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
File retrievedDB = new File(retreivedDBPAth);
Log.d("PATHS", " CurrentDB=" +
currentDBPath + "\n\t" + currentDB.getPath() +
"\n\tExists=" + String.valueOf(currentDB.exists()) +
"\nBackup=" + backupDBPath + "\n\t" + backupDB.getPath() +
"\n\tExists=" + String.valueOf(backupDB.exists()) +
"\nRetrieved DB=" + retreivedDBPAth + "\n\t" + retrievedDB.getPath() +
"\n\tExists=" + String.valueOf(retrievedDB.exists())
);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
//destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
}
ENOENT 不是针对数据库,而是根据 的外部存储:-
01-18 11:11:32.017 2689-2689/? D/PATHS: CurrentDB=/data/example.com.so48304909_spinner/databases/spinnerExample
/data/data/example.com.so48304909_spinner/databases/spinnerExample
Exists=true
Backup=/storage/extSdCard/mydatabase
/mnt/sdcard/storage/extSdCard/mydatabase
Exists=false
Retrieved DB=/data/data/example.com.so48304909_spinner/databases/spinnerExample
/data/data/example.com.so48304909_spinner/databases/spinnerExample
Exists=true
01-18 11:11:32.021 2689-2689/? W/System.err: java.io.FileNotFoundException: /mnt/sdcard/storage/extSdCard/mydatabase: open failed: ENOENT (No such file or directory)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:416)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-18 11:11:32.021 2689-2689/? W/System.err: at example.com.so48304909_spinner.MainActivity.exportDB(MainActivity.java:162)
01-18 11:11:32.021 2689-2689/? W/System.err: at example.com.so48304909_spinner.MainActivity.onCreate(MainActivity.java:38)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.Activity.performCreate(Activity.java:5008)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.access0(ActivityThread.java:130)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.os.Looper.loop(Looper.java:137)
01-18 11:11:32.021 2689-2689/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
01-18 11:11:32.021 2689-2689/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-18 11:11:32.021 2689-2689/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-18 11:11:32.021 2689-2689/? W/System.err: at dalvik.system.NativeStart.main(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.Posix.open(Native Method)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-18 11:11:32.021 2689-2689/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:400)
01-18 11:11:32.021 2689-2689/? W/System.err: ... 18 more