【问题标题】:SQlite MAX query returns empty sometimeSQlite MAX 查询有时返回空
【发布时间】:2014-09-17 12:49:44
【问题描述】:

我正在尝试在列中获取最大值。很多时候我能够获得最大值。但有几次没有返回

以下是使用日志执行的两个示例,我什么也没得到和价值

一无所获

-log-query=select MAX(id) from product WHERE price = ?

-log-args count=1

-log-args[0]=31

cursor=db.rawQuery(query, args);

-log-cursor.getCount()=1

if(cursor.moveToFirst())    

-log- 成功移动光标到第一行

-log-cursor.getString(0)=

-log-cursor.getType(0)=3

-log-cursor.getColumnName(0)=MAX(id)

获得价值

-log-query=select MAX(id) from product WHERE price = ?

-log-args count=1

-log-args[0]=31

Cursor cursor=db.rawQuery(query, args);

-log-cursor.getCount()=1

if(cursor.moveToFirst())    

-log- 成功移动光标到第一行

-log-cursor.getString(0)=3

-log-cursor.getType(0)=1

-log-cursor.getColumnName(0)=MAX(id)

【问题讨论】:

  • price 的数据类型是什么?
  • @GordonLinoff 价格数据类型为字符串
  • 每一行是否都有 price 和 id 的值?
  • @Karakuri 是的,每一行都有 price 和 id 的值
  • @Karakuri 你说得对。 id 字段中缺少值,网络同步后 id 字段中存储了空值。谢谢。

标签: android mysql ios sql sqlite


【解决方案1】:

使用 pricevalue 添加单引号,因为 price 是字符串类型,所以使用您的查询作为

select MAX(id) from product WHERE price = "'" + pricevalue + "'";

但推荐的解决方案是使用query 而不是rawQuerylike

cursor = db.query("product", new String [] {"MAX(id)"}, "price=?",
            new String[] { pricevalue }, null, null, null);

注意:pricevalue 将是价格的价值。

【讨论】:

  • 我已经尝试了第一个,我得到 cursor.getString(0) 的 null 值,并且 cursor.getType(0) 为 0,curso 计数为 1。
  • @user3551469 尝试推荐的解决方案和空值是什么?发布 logcat 异常。
【解决方案2】:

我怀疑问题是浮点转换之一。尝试将where 子句表达为:

where abs((price + 0) - pricevalue) < 0.005

您可能还会遇到价格问题,例如 '$5.00',但 pricevalue'5'。仅仅引用字符串是没有用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多