Reputation: 69
I have a database table, my goal is to read in each of the values from the data table, then write them to a text file to be emailed. How should I go about accomplishing this?
public void FileWrite()
{
Cursor remindersCursor = mDbHelper.fetchAllReminders();
startManagingCursor(remindersCursor);
try
{ // catches IOException below
String[] from = new String[]{RemindersDbAdapter.KEY_TITLE};
final String TESTSTRING = new String(RemindersDbAdapter.KEY_TITLE + " ");
File sdCard = Environment.getExternalStorageDirectory();
File myFile = new File(sdCard, "test");
FileWriter writer = new FileWriter(myFile);
writer.append(TESTSTRING);
writer.flush();
writer.close();
Toast.makeText(TaskReminderActivity.this, "Program Successfully went through FileWrite!", Toast.LENGTH_LONG).show();
} catch(Exception e)
{
Toast.makeText(TaskReminderActivity.this, "Had Problems with file!", Toast.LENGTH_LONG).show();
Log.e("FileWrite", "Had Problems with file!", e);
}
}
Upvotes: 0
Views: 7274
Reputation: 3747
First Reading from you sqllite Database :
Cursor cursor = db.rawQuery("SELECT * FROM " +TBL_NAME+" " ,null);
startManagingCursor(cursor);
while(cursor.moveToNext()){
stringBuffer.append(cursor.getString(1)).append(";");
}
......
Next Writing on the card :
try {
File myFile = new File("/sdcard/file.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(stringBuffer.toString());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
....
Make sure you have permission set in your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Upvotes: 1
Reputation: 3870
Just a simple example. Hope you can pick up easily
http://android-er.blogspot.in/2011/06/simple-example-using-androids-sqlite_02.html
Upvotes: 0