[英]How to export data from SQlite to CSV or Excel or just anything?
在研究如何從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此代碼不應導出任何內容。 它只是重寫相同的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.