我发现这种方法可以将db导出为CSV,但是当它创建一个文件时,它表示权限被拒绝我已经在清单中授予了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这是代码
private void exportDB() {
File dbFile = getDatabasePath("db_journal.db");
db = new JournalDB(getApplicationContext());
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "ojt.csv");
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase sql = db.getReadableDatabase();
Cursor curCSV = sql.rawQuery("SELECT * FROM logs", null);
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
String arrStr[] = {curCSV.getString(0), curCSV.getString(1), curCSV.getString(2),curCSV.getString(3), curCSV.getString(4), curCSV.getString(5), curCSV.getString(6)};
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
} catch (Exception sqlEx) {
Log.e("ProfileMain", sqlEx.getMessage(), sqlEx);
}
}