简体   繁体   English

如何将数据从SQlite导出到CSV或Excel或什么?

[英]How to export data from SQlite to CSV or Excel or just anything?

Doing research on how to export a database from SQLite, I already found the following code but was not able to make it work for me. 在研究如何从SQLite导出数据库时,我已经找到了以下代码,但无法使其对我有用。

myDbAdapter.java: 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: MainActivity.java:

public void export(View view){
    helper.exportDB(getApplicationContext());
}

public static void main(String[] args){}

When I press the Export Button it says "Export Failed". 当我按下“导出”按钮时,它显示“导出失败”。 Before I added the void main there was an error telling me that it is needed but I have no idea what to put into it. 在添加void main之前,有一个错误告诉我它是必需的,但是我不知道要放什么。 Additionally I don't think Environment.getExternalStorageDirectory() is the right way to find the directory since there is no SDCard in my Tablet. 另外,由于Tablet中没有SDCard,因此我认为Environment.getExternalStorageDirectory()不是查找目录的正确方法。 Any help appreciated! 任何帮助表示赞赏!

You can setup any destination for output file by yourself 您可以自己设置输出文件的任何目标

psThis code should not export anything. ps此代码不应导出任何内容。 It just rewrite the same data. 它只是重写相同的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM