代码之家  ›  专栏  ›  技术社区  ›  notbrain

为什么这个Anorm结果是一个空列表?(播放2.1.0)

  •  0
  • notbrain  · 技术社区  · 13 年前

    这里没有Scala;我一辈子都不明白为什么我没有得到这个Anorm SQL调用的结果。当我运行SQL调试输出时,它会返回一个很好的结果,但当我运行代码时,我会得到一个空的List()。

    我的RowParser有问题吗?为什么我在调试输出中看到了好的SQL,但我的 result 瓦尔?

    我的SQL中遗漏了什么吗 .as() 以正确地将结果行映射到Parser?当我删除最后一个 后果 排队 后果 val评估为一个单位,这肯定是可疑的。

    // Case class - SQL results rows go into List of these
    case class PerformanceData(
        date: String, 
        kwh: String
    )
    
    // RowParser
    val perfData = {
        get[String]("reading_date") ~ get[String]("kwh") map{ 
            case reading_date~kwh => PerformanceData(reading_date, kwh) 
        }
    }
    
    // SQL Call - function ret type is Seq[PerformanceData]
    DB.withConnection("performance") { implicit connection => 
    
        val result: Seq[PerformanceData] = SQL(
        """
            SELECT CONCAT(reading_date) AS reading_date,
               CONCAT(SUM(reading)) AS kwh
            FROM perf
            WHERE reading_date >= DATE_SUB(NOW(), INTERVAL 45 DAY)
            AND sfoid IN ({sf_account_ids})
            GROUP BY reading_date
            ORDER BY reading_date DESC
            LIMIT 30
        """
        ).on(
            'sf_account_ids -> getSQLInValues(SFAccountIDs)
        ).as(
            User.perfData *
        )
    
    //  Logger.debug(result.toString) -> EMPTY LIST!??
        result // Why is this necessary to return proper type?
    
    }
    
    2 回复  |  直到 13 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    不幸的是,您需要为in子句使用非绑定变量,而是替换字符串中的值。

    另请参阅: "In" clause in anorm?

    编辑:我的意思是 sf_account_ids 将是单绑定变量。大概 sfoid IN (?, ?, ?) 应为,但语句将为 sfoid IN (?) .

        2
  •  0
  •   korefn    13 年前

    对于第一个问题,我建议您检查 case 中的声明 perData 并确保其准确性。功能 getSQLInValues(...) 也可能是原因。

    关于为什么你需要最后一个 result 这是因为scala在未显式定义的情况下使用闭包中的最后一条语句来推断返回类型。所以 val result = SQ(...) 作为一项任务将返回 Unit

    为了避免这种情况,你可以这样做:

    DB.withConnection("performance") { implicit connection =>
      SQL(...).on(...).as(...)
    }
    

    通过不分配来自 SQL 它被用来推断类型。

    推荐文章