0

I have an app that stores data in a data base. Now, I want a button to export this data base into a text file.

Here is the code of my Export Button:

//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
  • 7,773
  • 14
  • 36
  • 45
Vhox
  • 17
  • 7

2 Answers2

0

You'll need to query the database using a method of the db object. Then you'll need to iterate those results and write them them to your file. Check out the methods available for this in the SQLiteDatabase documentation.

EDIT 1:

Try something like this:

Cursor cursor = db.query ("table_customer", new String[]{"name","phone", "email", "job", "others"},null,null,null,null,null);

//cursor is now at the first result returned by this query
do{
    int colIdx = cursor.getColumnIndex("name");
    String name = cursor.getString(colIdx);
    //do something with name
}while(cursor.moveToNext()); //loop will terminate when all the rows are exhausted

EDIT 2: Here's a link to an article which details how to use the query and cursor functions.

Jacob Calvert
  • 188
  • 1
  • 2
  • 11
  • Dear Jacob. Thanks for your time. Well, so sorry if I am asking too much... I guess I got the idea of what to do... But I don't know how to do it... I am really new at this, and believe that all my efforts are on make this happens. Could you write a kind of demonstration? Thanks a lot! – Vhox Apr 27 '15 at 02:22
  • I think you can probably do something like this. `Cursor cursor = db.query ("table_customer", new String[]{"name", "phone", "email", "job", "others"}, null,null,null,null,null); //cursor is now at the first result returned by this query do{ int colIdx = cursor.getColumnIndex("name"); String name = cursor.getString(colIdx); //do something with name }while(cursor.moveToNext()); ` – Jacob Calvert Apr 27 '15 at 02:46
  • Dear @Jacob, I guess I am finallying get into something, just because of your patience... Well... I uploaded my code coz there is no more space to write here... Do u think its correct? And how I write all the query inside the text file? Thanks again and again – Vhox Apr 27 '15 at 03:09
  • @Vhox, it looks correct, only problem is probably that the "_id_customer" fields was not selected in the query so that may not work. However, it seems correct. For writing a text file in android, [see here.](http://stackoverflow.com/questions/13779256/how-do-i-write-to-a-txt-file-in-android) – Jacob Calvert Apr 27 '15 at 03:49
  • yes, no error happens. But now, whats the command to save into the text file? I tried to use FileOutPuStream to create the text file. It worked, but I dont know how to write the databases in it... Could you help? – Vhox Apr 27 '15 at 04:42
  • I'm not sure what you mean by writing the databases in it. Are you trying to write SQL out to the text file? Or just the content of the database? – Jacob Calvert Apr 27 '15 at 20:50
0

What you could do is query each table in turn with a select all query and for each table use a StringBuilder to manually build either XML or CSV (comma separated value) files.

For example if a Persons table had the following colums {ID, FNAME, LNAME, AGE} then for a table containing two rows of data, I could generate the following for XML

<persons>
  <person>
    <ID>1</ID>
    <FNAME>Isaac</FNAME>
    <LNAME>Meacock</LNAME>
    <AGE>27</AGE>
  </person>
  <person>
    <ID>2</ID>
    <FNAMENorah</FNAME>
    <LNAME>Bone</LNAME>
    <AGE>52</AGE>
  </person>
<persons>

Or the following for CSV

1, Isaac, Meacock, 27
2, Norah, Bone, 53

Then it's just a matter of saving standard text files in Java.

Andrew S
  • 2,847
  • 3
  • 33
  • 50