我有一個將數據存儲在數據庫中的應用程序。現在,我想要一個按鈕將這個數據庫導出到一個文本文件中。Android - 在文本文件中導出數據庫
這裏是我的導出按鈕的代碼:
//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();
}
}
固定代碼格式並從問題中刪除不相關的部分 – Achrome