【问题标题】:Update data in sqlite use int array使用 int 数组更新 sqlite 中的数据
【发布时间】:2017-05-09 08:27:22
【问题描述】:
/*
<Update total page>
work: Update total page
input: title, new total page
output: none
*/

public void update_tot_page(String str_title, int int_newPageTot) {
    db = helper.getWritableDatabase(); 

    ContentValues values = new ContentValues();
    values.put("page_total", int_newPageTot);    
    //error->
    db.update("booklist", values, "page_total=?", new int[] {int_newPageTot});
    //<-
}


我想更新booklist 表中的总页数。
我发现 db.update("booklist", values, "title=?", new String[]{str_title}); 没错。
对了,为什么db.update("booklist", values, "page_total=?", new int[] {int_newPageTot});错了?
我应该如何更改这个句子

【问题讨论】:

    标签: android android-studio android-sqlite


    【解决方案1】:

    the last 2 parameters inupdate()whereClausewhereArgs,相当于where clause,例如where id = 10

    db.update("booklist", values, "title=?", new String[]{str_title});
    

    以上是正确的,因为您正在为page_total 设置新值,并且条件是where title=...

    下面的不正确,因为在 DB 中找不到 where page_total=newTotal syntax-wise 是正确的,但它不会匹配 db 中的记录。或者它可能与 total = 您尝试设置的新总数匹配,但这可能不是您真正想要的记录,坚持title 或使用主键(ID)

    db.update("booklist", values, "page_total=?", new int[] {int_newPageTot});
    

    如果你真的想用 page_total:where page_total=... 来做,你需要将 currentTotal 也作为参数传递给函数并在 update() 中使用它

    db.update("booklist", values, "page_total=?", new int[] {currentTotal});
    

    但同样,这可能会更新 2 个或 3 个甚至更多记录,因为它们都有 page_total 等于 currentTotal 参数,这可能是错误的(对你来说)

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 2021-03-24
      • 2021-04-07
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多