我见过如何导入和导出的android数据库中的几个帖子,我发现这些code,但我似乎无法使它发挥作用。我得到的错误java.io.filenotfoundexception /存储/ sdcard0 / BackupFolder /数据库名:打开失败ENOENT(没有这样的文件或目录)。香港专业教育学院改变了一些东西,但我仍然没有得到文件中发现异常
这是我的出口:
私人无效exportDB(){
尝试 {
db.open();
文件NEWFILE =新的文件(/ SD卡/ myexport);
输入的InputStream =新的FileInputStream(
/data/data/com.example.mycarfuel/data
基地/ MyDatabase的);
的OutputStream输出=新的FileOutputStream(NEWFILE);
byte []的缓冲区=新的字节[1024];
INT长;
而((长度= input.read(缓冲液))大于0){
output.write(缓冲液,0,长度);
}
output.flush();
output.close();
input.close();
db.close();
}赶上(FileNotFoundException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
和我的导入:
私人无效importDB(){
尝试 {
文件SD = Environment.getExternalStorageDirectory();
文件数据= Environment.getDataDirectory();
如果(sd.canWrite()){
字符串currentDBPath =//数据//+包名
+//数据库//+数据库名;
字符串backupDBPath =/ BackupFolder /数据库名
;
文件的某个backupdb =新的文件(数据,currentDBPath);
文件currentDB =新的文件(SD,backupDBPath);
FileChannel SRC =新的FileInputStream(currentDB).getChannel();
FileChannel DST =新的FileOutputStream(某个backupdb).getChannel();
dst.transferFrom(源,0,src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(),backupDB.toString(),
Toast.LENGTH_LONG).show();
}
}赶上(例外五){
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG)
。显示();
}
}
解决方案
SQLite数据库到本地文件系统 -
函数声明 -
尝试{
backupDatabase();
}赶上(IOException异常E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
功能定义 -
公共静态无效backupDatabase()抛出IOException异常{
//打开本地数据库作为输入流
字符串inFileName =/data/data/com.myapp.main/databases/MYDB;
文件DBFILE =新的文件(inFileName);
的FileInputStream FIS =新的FileInputStream(DBFILE);
字符串outFileName = Environment.getExternalStorageDirectory()+/ MYDB;
//打开空分贝的输出流
的OutputStream输出=新的FileOutputStream(outFileName);
//传输的字节从inputfile中的OUTPUTFILE
byte []的缓冲区=新的字节[1024];
INT长;
而((长度= fis.read(缓冲液))大于0){
output.write(缓冲液,0,长度);
}
//关闭流
output.flush();
output.close();
fis.close();
}
Ive seen a few posts on how to import and export a database in android and i found these code, but i cant seem to make it work. I get the error java.io.filenotfoundexception /storage/sdcard0/BackupFolder/DatabaseName:open failed ENOENT (no such file or directory). Ive changed a few things but i still get no file found exception
here is my export:
private void exportDB() {
try {
db.open();
File newFile = new File("/sdcard/myexport");
InputStream input = new FileInputStream(
"/data/data/com.example.mycarfuel/data
bases/MyDatabase");
OutputStream output = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
db.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and my import:
private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "PackageName"
+ "//databases//" + "DatabaseName";
String backupDBPath = "/BackupFolder/DatabaseName
";
File backupDB = new File(data, currentDBPath);
File currentDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(), backupDB.toString(),
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
}
解决方案
SQlite database to our local file system-
Function declaration-
try {
backupDatabase();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Function defined-
public static void backupDatabase() throws IOException {
//Open your local db as the input stream
String inFileName = "/data/data/com.myapp.main/databases/MYDB";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()+"/MYDB";
//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();
}
上一篇:如何获得自定义视图列表中选中的行自定义、视图、如何获得、列表中
下一篇:您的内容必须有一个ListView扩展ListActivity时,其ID属性为'android.R.id.list“您的、有一个、属性、内容