2013-01-24 58 views
0

我想對我的Android應用程序的sqlite數據庫進行更新,但在更新方法中出現錯誤。如何更新sqlite數據庫?

我的代碼:

public void addInfo(View view){ 

     EditText et = (EditText)findViewById(R.id.txtContacto); 

     String TAG = ((TextView)findViewById(R.id.textView2)).getText().toString(); 
     String FACH = ((TextView)findViewById(R.id.textView1)).getText().toString(); 

     ContentValues werte = new ContentValues(); 
     werte.put("info",et.getText().toString()); 

     mDatenbank.update(TAG, werte, "name="+FACH, null); 

     Toast.makeText(this, 
       getResources().getString(R.string.db_info_add), 
       Toast.LENGTH_SHORT).show(); 


    } 

TAG是我的tablename FACH是一列

表佈局:

ID(INT)|名稱(文本)| info(文本)

+0

請出示logcat的和你database.java類 – Opiatefuchs

+3

「我得到一個錯誤」是指絕對沒有除非你告訴我們**錯誤是什麼**。我們無法閱讀您的想法或從我們的位置看到您的屏幕,也無法看到logcat。如果您沒有提供任何信息,我們無法幫助您解決問題。 –

回答

1

如果FACH包含字符串文字所需的引號,那麼它會起作用,如String FACH="'I am quoted'"。它仍然是錯誤的:真正的解決辦法是使用whereArgs,這樣的事情:

mDatenbank.update(TAG,werte,"name=?",new String[] { FACH }); 

(免責聲明:我不使用Java,我的代碼可能是錯的離譜)。

0

在這裏你有一個很好的例子:http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

updateContact() 
    // Updating single contact 
public int updateContact(Contact contact) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, contact.getName()); 
    values.put(KEY_PH_NO, contact.getPhoneNumber()); 

    // updating row 
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", 
      new String[] { String.valueOf(contact.getID()) }); 
} 

最好是向我們展示了logcat的