【问题标题】:Convert Database into .csv file将数据库转换为 .csv 文件
【发布时间】:2015-04-27 00:04:29
【问题描述】:

我正在尝试将我的数据库转换为 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);
    }
}

}

【问题讨论】:

    标签: android database csv


    【解决方案1】:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-21
      • 2013-02-03
      • 2018-02-12
      • 2019-09-11
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多