【问题标题】:database update method unable to accept integer value数据库更新方法无法接受整数值
【发布时间】:2026-02-12 10:40:01
【问题描述】:

这是我之前在 db.update 上提出的问题。

我使用了 Karakuri 建议的代码并将数据库适配器方法修改为以下。 请注意,数据库适配器从 mainactivity 获得的整数(实际上是 34)中的 fma

    public void updateJ(int fma){
         String where = "coli = ?";

        ContentValues arw = new ContentValues();
        cv.put("coli", fma);

            the below no longer used but null the last argument instead
        //  String jusi =  Integer.toString(fma);
        // String[] whereArgs = new String[] {jusi};
        //Log.w(TAG, "whereArgs = " + whereArgs);


        db.update("superTable", cv, where, null);
            }

还是不行。 // 从之前的'log.w 读取为 06-02 16:24:31.695: W/DBA 信息(18940): whereArgs = [Ljava.lang.String;@5299a034'

//但是在阅读在线教程和android网站以及Karakuri的解释后我有一个问题,即第四个参数whereArgs应该是String[]而不是String或int。但是 superTable 中的“coli”列在设计上是一个整数。如何让它接受 int 而不是 String[]?

我已经将 whereargs 替换为 null 并且基本上使用 cv.put 直接使用 int fma 变量,但它仍然不起作用?在放入之前将int转换为字符串,它也不起作用。

【问题讨论】:

  • 你为什么首先把它变成一个数组?另外,您的“大肠杆菌”列是整数还是字符串?为什么要将 int 与字符串数组进行比较?
  • 大肠杆菌是一个整数。 db.update() 方法不接受 int 或字符串作为 whereArgs。 SQLiteDatabase 类型中的方法 update(String, ContentValues, String, String[]) 不适用于参数 (String, ContentValues, String, int)
  • 只是为了给你一些想法。 *.com/questions/9798473/… 和这里 *.com/questions/5987863/… --- 看到最后一个参数是 null :)))
  • 感谢您的建议。我刚刚尝试使用 'null' 而不是 'whereArgs' 但它仍然没有更新..
  • 我应该将 fma 转换为字符串吗? .. 唔 。从头开始。只是将其转换为字符串并重新插入,它仍然无法正常工作

标签: android android-sqlite


【解决方案1】:

还是不行。 // 从之前的'log.w 读取为 06-02 16:24:31.695: W/DBA 信息(18940): whereArgs = [Ljava.lang.String;@5299a034'

您没有将whereArgs 传递给update(),这就是它“不起作用”的原因。改变

db.update("superTable", cv, where, null);

db.update("superTable", cv, where, whereArgs);

日志输出是在 String[] 字符串数组上调用 toString() 时所期望的。

【讨论】: