mysqlscalaplayframeworkscalikejdbc

How can i execute the sql query with parameter?


val resultList: List[List[String]] =
  sql"""
       select * from exchange_transaction
     """
    .map(
      rs =>
        List(
          rs.string("transaction_id")
        )
    )
    .list()
    .apply()

execute the above code,it is ok but this sql "select * from exchange_transaction" is not fixed. I want this sql like a parameter ,like the follow(shorthand code.....)

def findMemberList(segmentExecuteSql: String
    val resultList: List[List[String]] =
      sql"""
           $segmentExecuteSql
         """
        .map(
          rs =>
            List(
              rs.string("transaction_id")
            )
        )
        .list()
        .apply()

but it's error after the execution the message is

[error] s.StatementExecutor$$anon$1 - SQL execution failed (Reason: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''select * from  exchange_transaction'' at line 1
Query is : 
           'select * from  exchange_transaction'
         ):

   'select * from  exchange_transaction'

what should i do ,how to fix the exception?


Solution

  • You can try turning resultList into a function:

    import scalikejdbc.SQL
    
    def resultList(sqlQuery: String): List[List[String]] =
      SQL(sqlQuery)
        .map(
          rs =>
            List(
              rs.string("transaction_id")
            )
        )
        .list()
        .apply()