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

2 回答 2

1

首先从你的 sqllite 数据库中读取:

Cursor  cursor = db.rawQuery("SELECT * FROM " +TBL_NAME+" " ,null);
startManagingCursor(cursor);
 while(cursor.moveToNext()){

                    stringBuffer.append(cursor.getString(1)).append(";");

          }
......

下一张写在卡片上:

  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();
    }

……

确保您在清单文件中设置了权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-06-27T18:41:23.460 回答
0

只是一个简单的例子。希望你能轻松上手

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

于 2012-06-27T17:54:24.377 回答