How to export database?

0

I need to export my bank to a text file. I've seen some questions like this here, but none with a cohesive or coherent answer.

Here is an excerpt from my code:

//ACAO DO BOTAO CADASTRAR
public void Cadastrar (View view) {

    //SALVO OS DADOS NO BANCO
    SQLiteDatabase db = banco.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("nome", nome.getText().toString());
    values.put("telefone", telefone.getText().toString());
    values.put("email", email.getText().toString());
    values.put("empresa", email.getText().toString());
    values.put("obs", obs.getText().toString());

    long resultado = db.insert("tab_clientes", null, values);


    try {


        //AQUI SERIA A PARTE DE EXPORTAR, MAS REALMENTE NÃO FAÇO IDEIA DE COMO FAZER
        FileOutputStream ArqTexto = new FileOutputStream(Environment.getExternalStorageDirectory() + "/Clientes.csv");

        Cursor cursor = db.query("tab_clientes", new String[]{"_id_cliente", "nome", "telefone", "email", "empresa", "obs"}, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                cliente = new HashMap<String, String>();
                cliente.put("_id_cliente", cursor.getString(cursor.getColumnIndex("_id_cliente")));
                cliente.put("nome", cursor.getString(cursor.getColumnIndex("nome")));
                cliente.put("telefone", cursor.getString(cursor.getColumnIndex("telefone")));
                cliente.put("email", cursor.getString(cursor.getColumnIndex("email")));
                cliente.put("empresa", cursor.getString(cursor.getColumnIndex("empresa")));
                cliente.put("obs", cursor.getString(cursor.getColumnIndex("obs")));

              }
              while (cursor.moveToNext()); 

    }

    catch (IOException e) {
        Toast.makeText(this, "Erro", Toast.LENGTH_SHORT).show();
    }


    if (resultado != -1) {
        Toast.makeText(this, "Cliente Cadastrado!", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(this, "Erro ao Cadastrar!", Toast.LENGTH_SHORT).show();
    }
}
    
asked by anonymous 27.04.2015 / 02:04

0 answers