将数据库转换为.csv文件

时间:2015-04-27 00:04:29

标签: android database csv

我正在尝试将我的数据库转换为csv文件,但我遇到了一些麻烦。我大约需要一年的编码时间,所以这对我来说非常困难。

这是我制作csv文件的代码。它生成文件,但由于某种原因,它使用数据库中的所有数据生成一个空文件而不是csv文件。

public class ExportDatabaseToCSV{

static Context context;
public ExportDatabaseToCSV(Context context) {
    this.context=context;
}

public static void exportDataBaseIntoCSV(){


    Database db = new Database(context);//here CredentialDb is my database. you can create your db object.
    File exportDir = new File(Environment.getExternalStorageDirectory(), "");

    if (!exportDir.exists())
    {
        exportDir.mkdirs();
    }

    File file = new File(exportDir, "csvfilename.csv");

    try
    {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        SQLiteDatabase sql_db = db.getReadableDatabase();//here create a method ,and return SQLiteDatabaseObject.getReadableDatabase();
        Cursor curCSV = sql_db.rawQuery("SELECT * FROM "+Database.DATABASE_TABLE,null);
        csvWrite.writeNext(curCSV.getColumnNames());

        while(curCSV.moveToNext())
        {
            //Which column you want to exprort you can add over here...
            String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)};
            csvWrite.writeNext(arrStr);
        }

        csvWrite.close();
        curCSV.close();
    }
    catch(Exception sqlEx)
    {
        Log.e("Error:", sqlEx.getMessage(), sqlEx);
    }
}

}

 try
    {

        File file = new File(exportDir, "csvfilename.csv");
        file .setReadable(true, false);
        file .setWritable(true,false);

        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        SQLiteDatabase sql_db = db.openOrCreateDatabase("community_service_Database", Context.MODE_PRIVATE, null);
        Cursor curCSV = sql_db.rawQuery("SELECT * FROM "+Database.DATABASE_TABLE,null);
        csvWrite.writeNext(curCSV.getColumnNames());

        while(curCSV.moveToNext())
        {
            //Which column you want to export you can add over here...
            String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)};
            csvWrite.writeNext(arrStr);

            csvWrite.close();
            curCSV.close();
        }
    }
    catch(Exception sqlEx)
    {
        Log.e("Error:", sqlEx.getMessage(), sqlEx);
    }
}

}

1 个答案:

答案 0 :(得分:0)

File构造函数已经为您创建了该文件。 您可能需要将其设置为可读写。

File file = new File(exportDir, "csvfilename.csv");
file .setReadable(true, false);
file .setWritable(true,false);

我建议您只扩展一个SQLiteOpenHelper类:

  public static class DBHelper extends SQLiteOpenHelper
  {
    public DBHelper(Context context)
    {
      super(context, "community_service_Database", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database)
    {
      String[] SqlStatements =
      {
        "CREATE TABLE TestTable (Id INTEGER PRIMARY KEY, test varchar(255) NOT null)"
      };

      for (int i = 0; i < SqlStatements.length; i++)
        database.execSQL(SqlStatements[i]);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }
  }

更改您的实施:

DBHelper db = new DBHelper(getApplicationContext());
//here CredentialDb is my database. you can create your db object.
File exportDir = new File(Environment.getExternalStorageDirectory(), "");

if (!exportDir.exists())
{
  exportDir.mkdirs();
}


try
{

    File file = new File(exportDir, "csvfilename.csv");
    file .setReadable(true, false);
    file .setWritable(true,false);

    CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
    SQLiteDatabase sql_db = db.getWritableDatabase();
    Cursor curCSV = sql_db.rawQuery("SELECT * FROM "+Database.DATABASE_TABLE,null);
    csvWrite.writeNext(curCSV.getColumnNames());

    while(curCSV.moveToNext())
    {
       //Which column you want to export you can add over here...
       String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)};
       csvWrite.writeNext(arrStr);

       csvWrite.close();
       curCSV.close();
    }
}
catch(Exception sqlEx)
{
    Log.e("Error:", sqlEx.getMessage(), sqlEx);
}