如何从sqlite数据库导出数据到Android应用程序的CSV文件

时间:2016-05-02 13:27:48

标签: android excel sqlite csv

我在这里看了一些类似的问题,但无法弄清楚如何使我的问题解决这个问题。我做了一个锻炼应用程序,并希望允许用户按下按钮将数据库中的数据导出到csv或excel文件。任何人都可以给我一些关于如何解决这个问题的建议。如果我需要再提供信息,请告诉我。感谢

这是我的ExportDatabaseCSVTask类:

public class ExportDatabaseCSVTask extends HomeScreen {
private final ProgressDialog dialog = new ProgressDialog(ExportDatabaseCSVTask.this);
@Override
protected void onPreExecute() {

    this.dialog.setMessage("Exporting database...");
    this.dialog.show();

}
protected String doInBackground(final String... args){


    File exportDir = new File(Environment.getExternalStorageDirectory(), "");
    if (!exportDir.exists()) {
        exportDir.mkdirs();
    }

    File file = new File(exportDir, "ExcelFile.csv");
    try {

        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));

        //data

        //Headers
       //database info in here??

        csvWrite.close();
        return "";
    }
    catch (IOException e){
        Log.e("MainActivity", e.getMessage(), e);
        return "";
    }
}

@SuppressLint("NewApi")
@Override
protected void onPostExecute(final String success) {

    if (this.dialog.isShowing()){
        this.dialog.dismiss();
    }
    if (success.isEmpty()){
        Toast.makeText(ExportDatabaseCSVTask.this, "Export successful!", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(ExportDatabaseCSVTask.this, "Export failed!", Toast.LENGTH_SHORT).show();
    }
}
}

这是我的DBAdapter类:

public class DBAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_WORKOUTDATE = "workoutDate";
public static final String KEY_EXERCISE_NOTES = "notes";

private static final String TAG = "WorkoutDBAdapter";
private DatabaseHelper mDBHelper;
private SQLiteDatabase mdb;

private static final String DATABASE_NAME = "WorkoutDB";
private static final String DATABASE_TABLE = "workouts";
private static final int DATABASE_VERSION = 2;

private final Context mCtx;

private static final String DATABASE_CREATE =
        "create table if not exists workouts " +
                "(_id integer primary key autoincrement, " +
                "title VARCHAR not null, " +
                "workoutDate date, " +
                "notes VARCHAR );";

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.w(TAG, DATABASE_CREATE);
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS workouts");
        onCreate(db);
    }
}


public DBAdapter(Context ctx) {
    this.mCtx = ctx;
}


public DBAdapter open() throws SQLException {
    mDBHelper = new DatabaseHelper(mCtx);
    mdb = mDBHelper.getWritableDatabase();
    return this;
}

public void close() {
    if (mDBHelper != null) {
        mDBHelper.close();
    }
}

//---insert a record into the database---
public long insertRecord(String title, String workoutdate, String notes) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_WORKOUTDATE, workoutdate);
    initialValues.put(KEY_EXERCISE_NOTES, notes);

    return mdb.insert(DATABASE_TABLE, null, initialValues);
}


//---retrieves all the records---
public Cursor getAllRecords() {
    Cursor mCursor = mdb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_TITLE,
            KEY_WORKOUTDATE, KEY_EXERCISE_NOTES}, null, null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

public boolean deleteRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return mdb.delete(DATABASE_TABLE, where, null) != 0;

}
}

0 个答案:

没有答案