簡體   English   中英

無法更新數據庫[sqlite]中的項目

[英]Can't update item in database [sqlite]

在代碼中找不到錯誤。 我敢肯定,ID​​可能存在一些問題。 也許查詢是錯誤的。 有點新的東西。

MyDBHelper

public void onClick(DialogInterface dialog, int whichButton) {
                String EditTextValue = edittext.getText().toString();
                Task t = new Task(EditTextValue);
                dbHandler.updateTask(t);


                if (dbHandler.updateTask(t) == true){
                    Toast.makeText(MainActivity.this, "IR", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "naah", Toast.LENGTH_LONG).show();
                }
                  displayTaskList();
             }

更新功能:

 public boolean updateTask(Task t){
    boolean result = false;
    String q = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + " = " + t.getID();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery(q, null);
    if (c.moveToFirst())
    {
        String q2 = "UPDATE " + TABLE_NAME + " SET " + COLUMN_TASK + " = "
                + t.getTASK() + " WHERE " + COLUMN_ID + " = " + t.getID();
        db.execSQL(q2);
        result = true;
    }
    db.close();
    return result;
}

任務類別:

public class Task {
private int _id;
private String _task;

public Task() {}
public Task(String task){
    this._task = task;
}
public Task (int id, String task){
    this._id = id;
    this._task = task;
}

// SETS&GETS
public void setID(int id) {
    this._id = id;
}
public int getID() {
    return this._id;
}
public void setTASK(String task){
    this._task = task;
}
public String getTASK(){
    return this._task;
}
}

主要活動:

// To detect which list item is selected (by ID)
private long _id;
public void setID(long id) { this._id = id; }
public long getID() { return this._id; }

// Attaches a long click listener to the listview
private void setupListViewListener() {
    lvTasks.setOnItemLongClickListener(
            new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapter,
                                               View item, int pos, long id) {
                    item1.setVisible(true); item2.setVisible(true); item3.setVisible(true); // Showing all buttons
                    setID(id); //Catches the selected items ID
                    return true;
                }

            });
}

問題可能在於它混淆了ID? 例如,在Mainactivity中,我得到的ID是選擇的任務(onlongclick),但在Task / MyDBHelper類中,ID是新創建的任務-即使它們未連接(?)。 任何幫助都會很棒。

您可以使用以下更新功能:

 public int updateTask(Task t){
 //i assume you open your db here
 SQLiteDatabase db = this.getWritableDatabase();

 //put values that needs to be updated in values with column names and  values
 ContentValues values = new ContentValues();
 values.put(COLUMN_TASK,t.getTASK());
 values.put(COLUMN_ID,t.getID());

 // db.update(String table,ContentValues values,String whereClause,String[] whereArguments)
 int result = db.update(TABLE_NAME,values,COLUMN_ID+"=?"+new String[] {String.ValueOf(t.getID())});
 db.close();
 return result;
 }

我想這可以解決您的問題。

暫無
暫無

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

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