0

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);
    }
}
4
  • marakana.com/forums/android/examples/55.html Commented Jun 27, 2012 at 17:55
  • @Sam well I am new to this so I am not really sure where to begin. I would think I would create a loop to read in all of the values of the database, but I am not sure how to attempt the code behind that kind of logic haha. I will post an edit of my file reading so far if you wold not mind giving me a little feedback. Commented Jun 27, 2012 at 18:02
  • 1
    Fabulous, when you provide more details in your question, you're more likely to get a viable answer. So, does this work? Do you have a file named "test" with the appropriate content on your SD card? Commented Jun 27, 2012 at 18:12
  • @Sam it is all working now, thanks to the link provided by Chintan Ragwani Commented Jun 27, 2012 at 18:46

2 Answers 2

1

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"/>
Sign up to request clarification or add additional context in comments.

Comments

0

Just a simple example. Hope you can pick up easily

http://android-er.blogspot.in/2011/06/simple-example-using-androids-sqlite_02.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.