I have an app that stores data in a data base. Now, I want a button to export this data base into a text file.
Here is the code of my Export Button:
//BUTTON ACTION
public void Save(View view) {
//Saving the data into the data base
SQLiteDatabase db = DataBase.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name.getText().toString());
values.put("phone", phone.getText().toString());
values.put("email", email.getText().toString());
values.put("job", job.getText().toString());
values.put("others", others.getText().toString());
long result= db.insert("table_customer", null, values);
Cursor cursor = db.query ("table_customer", new String[]{"name", "phone", "email", "job", "others"}, null,null,null,null,null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> customer = new HashMap<String, String>();
cliente.put("_id_customer", cursor.getString(cursor.getColumnIndex("_id_customer")));
cliente.put("name", cursor.getString(cursor.getColumnIndex("name")));
cliente.put("phone", cursor.getString(cursor.getColumnIndex("phone")));
cliente.put("email", cursor.getString(cursor.getColumnIndex("email")));
cliente.put("job", cursor.getString(cursor.getColumnIndex("job")));
cliente.put("others", cursor.getString(cursor.getColumnIndex("others")));
}
while (cursor.moveToNext());
}
cursor.close();
if (resultado != -1) {
Toast.makeText(this, "Customer saved and file exported", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Error 2", Toast.LENGTH_SHORT).show();
}
}