Export the SQLITEDB into .csv or .pdf
If you want to export the sqlite db into .csv or .pdf. You are going use the following
libraries
Task of Creating CSV
public class ExportCSVTask extends AsyncTask<String, Void,
Boolean> {
private final
ProgressDialog dialog = new ProgressDialog(
MainActivity.this);
protected void
onPreExecute() {
this.dialog.setMessage("Exporting
database...");
this.dialog.show();
}
@Override
protected
Boolean doInBackground(String... params) {
File dbFile
= getDatabasePath("basic_db");
File
exportDB = new File(Environment.getExternalStorageDirectory(),
"");
if
(!exportDB.exists()) {
exportDB.mkdirs();
}
File file =
new File(exportDB, getDateTime() + ".csv");
try {
file.createNewFile();
CSVWriter
csvWriter = new CSVWriter(new FileWriter(file));
SQLiteDatabase
db = dbHelper.getReadableDatabase();
Cursor
curCSV = db.rawQuery("SELECT * FROM basic_table", null);
csvWriter.writeNext(curCSV.getColumnNames());
while
(curCSV.moveToNext()) {
String
arrStr[] = { curCSV.getString(0),
curCSV.getString(1),
curCSV.getString(2),
curCSV.getString(3),
curCSV.getString(4) };
csvWriter.writeNext(arrStr);
}
csvWriter.close();
curCSV.close();
return
true;
} catch
(SQLException sqlEx) {
Log.e("MainActivity",
sqlEx.getMessage(), sqlEx);
return
false;
} catch
(IOException e) {
Log.e("MainActivity",
e.getMessage(), e);
return
false;
}
}
protected void
onPostExecute(final Boolean success) {
if
(this.dialog.isShowing()) {
this.dialog.dismiss();
}
if
(success) {
Toast.makeText(MainActivity.this,
"Export File successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"Export failed",
Toast.LENGTH_SHORT).show();
}
}
}
Task of Creating PDF
public class
ExportPDFTask extends AsyncTask<String, Void, Boolean> {
private final
ProgressDialog dialog = new ProgressDialog(
MainActivity.this);
protected void
onPreExecute() {
this.dialog.setMessage("Exporting
database...");
this.dialog.show();
}
@Override
protected
Boolean doInBackground(String... arg0) {
// TODO
Auto-generated method stub
SQLiteDatabase
db = dbHelper.getWritableDatabase();
Cursor c1 =
db.rawQuery("SELECT * FROM basic_table", null);
File dbFile
= getDatabasePath("basic_db");
File
exportDB = new File(Environment.getExternalStorageDirectory(),
"");
Document
document = new Document();
if
(!exportDB.exists()) {
exportDB.mkdirs();
}
File file =
new File(exportDB, getDateTime() + ".pdf");
try {
PdfWriter.getInstance(document,
new FileOutputStream(file));
document.open();
Paragraph
header = new Paragraph();
header.add("PDF
Export Example by using iTextPDF");
PdfPTable
pdfPTable = new PdfPTable(5);
pdfPTable.addCell("ID");
pdfPTable.addCell("NAME");
pdfPTable.addCell("AGE");
pdfPTable.addCell("ADDRESS");
pdfPTable.addCell("BLOODGROUP");
while
(c1.moveToNext()) {
String
_id = c1.getString(0);
String
c_name = c1.getString(1);
String
c_age = c1.getString(2);
String
c_address = c1.getString(3);
String
c_bgroup = c1.getString(4);
pdfPTable.addCell(_id);
pdfPTable.addCell(c_name);
pdfPTable.addCell(c_age);
pdfPTable.addCell(c_address);
pdfPTable.addCell(c_bgroup);
}
document.add(pdfPTable);
document.addCreationDate();
document.close();
return
true;
} catch
(FileNotFoundException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
return
false;
} catch (DocumentException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
return
false;
}
}
protected void
onPostExecute(final Boolean success) {
if
(this.dialog.isShowing()) {
this.dialog.dismiss();
}
if
(success) {
Toast.makeText(MainActivity.this,
"Export File successfully",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"Export failed",
Toast.LENGTH_SHORT).show();
}
}
}
You can also download the source from Github
Also download the app from Google Play
Comments