我有一个数据库表,我的目标是读取数据表中的每个值,然后将它们写入要通过电子邮件发送的文本文件。我该怎么做呢?
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);
}
}
答案 0 :(得分:1)
首先阅读sqlite数据库:
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"/>
答案 1 :(得分:0)