SQLite的出口为CSV [英] export sqlite into csv

查看:119
本文介绍了SQLite的出口为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从我SQLite数据库提取数据,并将其写入到csv文件并通过电子邮件发送的信息时,进行笔迹。只有一个表中的database.i 3列已DBAdapter和类有按钮的用户必须preSS才能导出数据。

im trying to take data from my sqlite database and write it into csv file and email the information when done writing.i only have one Table with 3 columns in the database.i have DBAdapter and a class that have the Button that the user must press to be able to export the data.

下面是codeI试图 Exportdata.java

Here is the code i have tried Exportdata.java

try {
    root = Environment.getExternalStorageDirectory();                   
    Log.i(TAG,"path.." +root.getAbsolutePath());  

    //check sdcard permission  
    if (root.canWrite()) {  
        File fileDir = new File(root.getAbsolutePath()+"/fun/");  
        fileDir.mkdirs();  

        //   Log.d("DATABASE", db.getAllBYname());

        File file= new File(fileDir, "itisfun.csv");  
        FileWriter filewriter = new FileWriter(file);  
        BufferedWriter out = new BufferedWriter(filewriter);  
        out.write("I m enjoying......dude..............."  ); 
        out.close();  
    }  
} catch (IOException e) {  
    Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());  
} 

这code确实造成SD卡中的文件,但问题是我不能让这把SQLite数据库的数据,并在文件中写上所示的code.im卡住,请帮助我的例子或编辑我的code与合适的解决方案。

This code does create the file in SDcard but the problem is i cant make this to take sqlite database data and write it in the file as shown on the code.im stuck please help me with example or edit my code with the right solution.

推荐答案

我用这个程序SQLite数据库转储到SD卡中的CSV格式的文件:

I use this routine to dump an SQLite database to a file on the SD card in CSV format:

private Boolean backupDatabaseCSV(String outFileName) {
    MyLog.d(TAG, "backupDatabaseCSV");
    Boolean returnCode = false;
    int i = 0;
    String csvHeader = "";
    String csvValues = "";
    for (i = 0; i < GC.CURCOND_COLUMN_NAMES.length; i++) {
        if (csvHeader.length() > 0) {
            csvHeader += ",";
        }
        csvHeader += "\"" + GC.CURCOND_COLUMN_NAMES[i] + "\"";
    }

    csvHeader += "\n";
    MyLog.d(TAG, "header=" + csvHeader);
    dbAdapter.open();
    try {
        File outFile = new File(outFileName);
        FileWriter fileWriter = new FileWriter(outFile);
        BufferedWriter out = new BufferedWriter(fileWriter);
        Cursor cursor = dbAdapter.getAllRows();
        if (cursor != null) {
            out.write(csvHeader);
            while (cursor.moveToNext()) {
                csvValues = Long.toString(cursor.getLong(0)) + ",";
                csvValues += Double.toString(cursor.getDouble(1))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(2))
                        + ",";
                csvValues += "\"" + cursor.getString(3) + "\",";
                csvValues += Double.toString(cursor.getDouble(4))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(5))
                        + ",";
                csvValues += "\"" + cursor.getString(6) + "\",";
                csvValues += Double.toString(cursor.getDouble(7))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(8))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(9))
                        + "\n";
                out.write(csvValues);
            }
            cursor.close();
        }
        out.close();
        returnCode = true;
    } catch (IOException e) {
        returnCode = false;
        MyLog.d(TAG, "IOException: " + e.getMessage());
    }
    dbAdapter.close();
    return returnCode;
}

GC是我的全局常量类,除其他外包含表的列名。列名被用来制造CSV文件标题行。的getAllRows是在数据库中适配器和返回所有表中的行。在同时转储全部被返回的行。长值用逗号分隔的文本值的引用,以及用逗号分隔。所述MyLog.d确实在测试模式下一个Log.d并执行的生产模式无关时。该dbAdapter的功能之外作为一个全局变量定义的:

GC is my Global Constants class which among other things contains the table column names. The column names are used to make the header row in the CSV file. The getAllRows is in the database adapter and returns all the rows in the table. The while dumps all the rows that were returned. The long values are comma separated and the text values are quoted as well as separated by a comma. The MyLog.d does a Log.d in test mode and does nothing when in production mode. The dbAdapter is defined outside of the function as a global variable:

DatabaseAdapter dbAdapter = null;

这是的onCreate初始化为:

It is initialized in onCreate as:

dbAdapter = new DatabaseAdapter(getApplicationContext());

的dbAdapter用于在活动的多种功能。如果全局定义你的数据库适配器,一定要对的所有dbAdapter.open()与dbAdapter.close()每当你使用它。另外,不要忘记关闭所有的游标打开。

The dbAdapter is used in multiple functions in the activity. If you globally define your Database adapter, be sure you pair every dbAdapter.open() with a dbAdapter.close() whenever you use it. Also, don't forget to close every cursor you open.

注:csvValues​​是SQLite的每一行中返回的列的串联。对于SQLite的游标返回的每一行,csvValues​​写成一个行的CSV表。

Note: csvValues is a concatenation of the columns returned in each SQLite row. For each row returned in the SQLite cursor, csvValues is written as a row in the CSV table.

这篇关于SQLite的出口为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆