在研究如何从SQLite导出数据库时,我已经找到以下代码,但无法使其对我有用。
myDbAdapter.java:
public static void exportDB(Context context) {
String databasePath = context.getDatabasePath(myDbHelper.DATABASE_NAME).getPath();
String inFileName = databasePath;
try {
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory() + "/" + myDbHelper.DATABASE_NAME;
OutputStream output = new FileOutputStream(outFileName);
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();
} catch (Exception e) {
Toast.makeText(context, "Export Failed", Toast.LENGTH_SHORT).show();
}
}
MainActivity.java:
public void export(View view){
helper.exportDB(getApplicationContext());
}
public static void main(String[] args){}
当我按下“导出”按钮时,它显示“导出失败”。
在添加void main之前,有一个错误告诉我它是必需的,但是我不知道要放什么。
另外,由于Tablet中没有SDCard,因此我认为Environment.getExternalStorageDirectory()不是查找目录的正确方法。
任何帮助表示赞赏!
最佳答案
您可以自己设置输出文件的任何目标
ps。此代码不应导出任何内容。它只是重写相同的数据。