2012-09-06 31 views
0

當按鈕被按下時,我想的文本以更改到數據庫collumn值,我知道它錯了,但這裏是代碼:更改文本到數據庫值

private void MostraDados() { 
    // TODO Auto-generated method stub 
    final TextView text = (TextView) findViewById(R.id.tvUSUARIO); 

    Button mostrar = (Button) findViewById(R.id.bMostrar); 
    mostrar.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      db = openOrCreateDatabase("dbtest.db", Context.MODE_PRIVATE, null); 

      String q = "SELECT * FROM dbtest.db WHERE usuarioorigem='"; 

      text.setText(q); 


      //text.execSQL("DROP COLUMN IF EXISTS usuarioorigem"); 
     } 
    }); 
} 

回答

0

您的代碼缺少例如管理光標和數據庫DatabaseClass一些關鍵部分。

private void MostraDados() { 

final TextView text = (TextView) findViewById(R.id.tvUSUARIO); 

Button mostrar = (Button) findViewById(R.id.bMostrar); 
mostrar.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     // our missing a database helper 
     MyDatabaseClass dbhelper = new MyDatabaseClass(); 
     dbhelper.open(); 

     Cursor result = dbhelper.doMyQuery(); 
     String mystring = result.getString(0); 

     text.setText(mystring); 

     dbhelper.close(); 
    } 
}); 
.... 
public class WorkoutDbAdapter { 
    .... 
    public Cursor doMyQuery() 
    { 
     return this.mDb.query(yourQuery); 
    } 

} 

這是你需要的最低限度,即使有上述我錯過了很多較小的細節。搜索關於創建和使用數據庫的一些教程。

從本質上講,您需要取回光標,設置光標位置或cursor.moveNext(),然後獲取您可以分配給textField的值。

您的源代碼缺少對數據庫的正確調用並訪問遊標。希望你會找到一些體面的教程,將其餘的爲你打實。

0

的SQL不正確書寫。您必須從列中選擇。並且您將查詢字符串傳遞給文本視圖。您應該首先查看如何使用遊標查詢數據庫,以及如何從光標中檢索所需內容。

因此,我會研究如何使用curosr,所有這些都可以在Android文檔中找到,並且您可能想要在模擬器中嘗試API演示程序。我確信您可以學習如何使用光標在那裏工作以及。所以看看這裏,http://developer.android.com/reference/android/database/Cursor.html

而在這裏,Is Android Cursor.moveToNext() Documentation Correct?

+0

你能幫我解釋一下我的代碼嗎?以及我嘗試了一些東西,但仍然給我的setText錯誤。 \t \t \t \t cursor = db.rawQuery(「SELECT * FROM dbtest.db WHERE usuarioorigem」,null,null); \t \t \t \t \t \t \t \t usuario.setText(光標); –

0

得到光標後,你可以做這樣的事情:

while(c.moveToNext(){ 

text.setText(c.getString(0)) 

}