【问题标题】:SQLiteDatabase update not working?SQLiteDatabase 更新不起作用?
【发布时间】:2013-01-10 01:39:49
【问题描述】:

我试图让 db.update 用新值更新我的数据库的其中一行,但它似乎没有保存。我查看了我的语法,但似乎无法保存数据。插入功能正在工作但没有更新。任何帮助,将不胜感激。下面是我使用 DbHelper 对象的数据类。

  public class Data {
    static final String TAG = "Data";

    public static final String DB_NAME = "data.db";
    public static final String TABLE_NAME = "tasks";
    public static final String C_ID = "_id";
    public static final String C_TASK = "taskname";
    public static final String C_DUE_DATE = "due_date";
    public static final String C_PRIORITY = "priority";
    public static final int DB_VERSION = 1;

    Context context;
    DbHelper dbHelper;
    SQLiteDatabase db;

    public Data(Context context) {
        this.context = context;
        dbHelper = new DbHelper();
    }

    public void insert(ToDoItem task) {
        db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(C_TASK, task.getTask());
        values.put(C_PRIORITY, task.getPriority());

        db.insert(TABLE_NAME, null, values);
    }
    public void update(int id, ToDoItem task) {
    ContentValues values = new ContentValues();
    values.put(C_ID, id);
    values.put(C_TASK, task.getTask());
    values.put(C_PRIORITY, task.getPriority());

    String[] whereArgs = {String.valueOf(id)};

    db.update(TABLE_NAME, values, C_ID +" =?", whereArgs);
    System.out.println(db.update(TABLE_NAME, values, C_ID +" =?", whereArgs));

    Log.d(TAG, "updating task " + db.toString() +" "+ id + " to priority " + task.getPriority());
}

    public void delete(int id){
        db.delete(TABLE_NAME, C_ID+"="+id, null);
    }

    public Cursor queueAll(){
        db = dbHelper.getWritableDatabase();

        String[] columns = new String[]{C_ID, C_TASK, C_DUE_DATE, C_PRIORITY};

        Cursor cursor = db.query(TABLE_NAME, columns,   
                null, null, null, null, null);
        return cursor;
     }

    class DbHelper extends SQLiteOpenHelper {

        public DbHelper() {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = String.format("create table %s" +
                        "(%s int primary key, %s text, %s text, %s int)", 
                        TABLE_NAME, C_ID, C_TASK, C_DUE_DATE, C_PRIORITY);

            Log.d(TAG, "onCreate with SQL:"+sql);

            db.execSQL(sql);    
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop if exists " + TABLE_NAME);
            onCreate(db);
        }
    }

}

这是我尝试在单击项目时更新优先级的 ToDoItem 对象类

     public class ToDoItem {
        private String task;
        private String dueDate;
        private int priority;

        public ToDoItem(String task) {
        this.task = task;
        this.priority = 1;
    }

    public ToDoItem(String task, String dueDate) {
        this.task = task;
        this.dueDate = dueDate;
        this.priority = 1;
    }

    public ToDoItem(String task, int priority) {
        this.task = task;
        if(priority <=3 && priority > 0) {
            this.priority = priority;
        } else 
            this.priority = 1;
    }

    public ToDoItem(String task, String dueDate, int priority) {
        this.task = task;
        this.dueDate = dueDate;
        if(priority <=3 && priority > 0) {
            this.priority = priority;
        } else 
            this.priority = 1; 
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public String getDueDate() {
        return dueDate;
    }

    public void setDueDate(String dueDate) {
        this.dueDate = dueDate;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        if(priority <=3 && priority > 0) {
            this.priority = priority;
        } else 
            this.priority = 1;
    }
}

最后是我的 onListItemClickListener,它使用 ToDoItems 的 ArrayList,更改优先级并从我的数据类调用更新方法。但是,我尝试更新的行的值保持不变。我使用 Log.d 来检查是否传递了正确的值,并且它们是。由于某种原因,这些变化没有发生。任何帮助将不胜感激。

        todoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
                long arg3) {

            if(soundEnabled) {
                playSound();
            }

            // Change the priority of the item inside the arrayList todoItems
            todoItems.get(arg2).setPriority(todoItems.get(arg2).getPriority() + 1);
            String name = todoItems.get(arg2).getTask();
            int priority = todoItems.get(arg2).getPriority();
            final ToDoItem task = new ToDoItem(name, priority);

            // Get the position of the task in the database
            int id = arg2 + 1;
            data.update(id, task);


});

【问题讨论】:

    标签: android database sqlite listview


    【解决方案1】:

    你会打印 db.update(TABLE_NAME, values, C_ID +"="+id, null) 函数返回值吗?

    如果返回值为0,则DB中没有这样的“id”行记录。

    【讨论】:

    • 我的分数为零,我会考虑更改 ID。
    • id 参数是正确的值,但是当我打印出 update() 语句时仍然返回 0。
    • 好的,我找到了数据库更新不起作用的原因。 "db.update(TABLE_NAME, values, C_ID +"=?"+id, null);"第二个和第三个参数是错误的。你可以这样做 "String[] whereArgs = {String.valueOf(id)};db.update(TABLE_NAME, values, C_ID +"=?", whereArgs );"
    • 感谢您,问题现已解决。你为我指明了正确的方向。
    • 但是你是怎么解决的? @WayneBJackson 我在帖子上浪费时间了吗?
    【解决方案2】:

    我看不到您如何创建表“任务”及其属性。但是,如果您更新所有属性,您可能会尝试使用 replace() 方法。尝试更换你的

    public void update(int id, ToDoItem task) {
    ...
    }
    

    与:

    public void update(int id, ToDoItem task) {
        ContentValues values = new ContentValues();
        values.put(C_TASK, task.getTask());
        values.put(C_PRIORITY, task.getPriority());
        db.replace(TABLE_NAME, null, initialValues);
    }
    

    还要确保 task.getTask() 返回存在于“任务”表中的正确整数值。您使用 int 参数调用 update() 方法,但是您从不在更新方法中使用它。 在您的更新方法中,您可能想要替换:

    values.put(C_TASK, task.getTask());
    

    values.put(C_TASK, id);
    

    【讨论】:

    • id参数是数据库中的行位置,task.getTask()返回用户添加的任务的名称。
    • 确保 task.getTask() 返回正确的行位置
    • 你是否替换了 values.put(C_TASK, task.getTask());用 values.put(C_TASK, id);正如我在回答中建议的那样?正如我所说,您正在传递 id 但您没有使用它。
    • 我现在可以看到更新了。是的。使用内容值。 system.out.println 打印 id 吗?如果你用 replace() 替换 update() 怎么样?
    • 我将如何执行替换来更新特定行。我添加了,它正在添加一个具有正确优先级的新条目,但不会替换现有条目。我会更新我的代码。
    【解决方案3】:

    我解决了这个问题,我在创建任务时没有读取 C_ID,因此我在创建数据库时将“整数主键”更改为“整数主键自动增量”,这意味着更新现在可以更新正确的行.感谢所有帮助我解决这个问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 2010-10-03
      • 2016-08-04
      • 2015-08-24
      相关资源
      最近更新 更多