2015-04-27 18 views
0

我有一個將數據存儲在數據庫中的應用程序。現在,我想要一個按鈕將這個數據庫導出到一個文本文件中。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(); 
    } 
} 
+0

固定代碼格式並從問題中刪除不相關的部分 – Achrome

回答

0

你需要查詢使用db對象的方法的數據庫。然後你需要迭代這些結果並將它們寫入你的文件。在SQLiteDatabase文檔中查看可用的方法。

編輯1:

嘗試是這樣的:

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 

編輯2: 這裏有一個link to an article詳細介紹瞭如何使用查詢和光標的功能。

+0

親愛的雅各布。謝謝你的時間。那麼,如果我問得太多,那麼很抱歉...我想我有想法做什麼......但我不知道該怎麼做......我真的很新,並且相信所有人我的努力是在做這件事情。你能寫一種示範嗎?非常感謝! – Vhox

+0

我想你可以做這樣的事情。 '遊標cursor = db.query(「table_customer」,new String [] {「name」,「phone」,「email」,「job」,「others」},null,null,null,null,null); //遊標現在處於由此查詢返回的第一個結果 do int colIdx = cursor.getColumnIndex(「name」); String name = cursor.getString(colIdx); //用名稱做某事 } while(cursor.moveToNext()); ' –

+0

親愛的@Jacob,我想我終於進入了一些東西,只是因爲你的耐心......好吧......我上傳了我的代碼,因爲沒有更多空間可以寫在這裏......你認爲它是正確的嗎?以及我如何在文本文件中寫入所有查詢?再次感謝 – Vhox

0

你可以做的是依次選擇查詢每個表,併爲每個表使用StringBuilder手動構建XML或CSV(逗號分隔值)文件。

例如,如果一個人的表具有下面的colums {ID,FNAME,L-NAME,AGE}然後用於包含數據的兩行的表,我可以生成用於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> 

或下述for CSV

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

然後,它只是一個保存在Java中的標準文本文件的問題。

相關問題