簡體   English   中英

如何在SQLite數據庫中更新一行的值

[英]How to update values on a row in SQLite database

我必須在活動中更新表的一行。 我使用類DatabaseHelper的方法擴展了SQLiteOpenHelper 這是屬於類DatabaseHelper的方法:

 public void updateQuantitaProdotto(SQLiteDatabase db,String Nome, String Quantita) {
        db.execSQL("update " + ProdottiTable.TABLE_NAME + " set " + ProdottiTable.QUANTITA + "= " + Quantita + "where "
                + "Nome = '" + Nome + "'");

    }

我需要傳遞三個參數,但是我不知道哪個值作為第一個參數。 當我想創建一個SQLiteDatabase對象時,在編輯器上出現錯誤,也許我不能使用它。

public void afterTextChanged(Editable s) {
              try {
                  if(quantitaSceltaEditText.getText().toString().isEmpty()==false) {
                      int quantitaSceltaInt = Integer.parseInt(quantitaSceltaEditText.getText().toString());
                      String prezzoTotale = String.valueOf(String.format("%.2f", (calcoloPrezzoTotale())));

                      prezzoTotateTextView.setText(prezzoTotale); 
                    var.aggiornaQuantitaProdotto(db,nomeTextView.getText().toString(),String.valueOf(nuovaQuantita));      //which value for db?
                  }
                  else
                      prezzoTotateTextView.setText("0,00");
              }
              catch (Exception e) {};`
            }

如果此方法在DatabaseHelper類內部,則無需從Activity傳遞db

另外,您還有一個串聯問題, =whereNome之間應該有空格。

應該是這樣的:

//Notice no db in the parameters for the method
public void updateQuantitaProdotto(String Nome, String Quantita) {

        //open the database if not done so already            
        SQLiteDatabase db = this.getWritableDatabase();

        //notice the extra spaces below 
        db.execSQL("update " + ProdottiTable.TABLE_NAME + " set " + ProdottiTable.QUANTITA + " = " + Quantita + " where "
                + " Nome = '" + Nome + "'");

        db.close(); //close the db if not needed anymore
    }

在不使用db參數的情況下從Activity調用:

//no value for db needed
var.aggiornaQuantitaProdotto(nomeTextView.getText().toString(),String.valueOf(nuovaQuantita));                      

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM