Search code examples
scalastring-formattingstring-interpolation

Escape quotes in string interpolation s"ALTER TABLE ${keyspace}.\"${tableName}\""


In Scala, while querying Cassandra, this string interpolation

s"ALTER TABLE ${keyspace}.\"${tableName}\" "

gives me this error:

error: value $ is not a member of String [INFO] val query:String=s"ALTER TABLE ${keyspace}.\"${tableName}\" ADD $colName $dataTypeAsString;"

What am I doing wrong?


Solution

  • The \" does not work inside string interpolations.

    Try using strings delimited by triple quotes:

    s"""ALTER TABLE ${keyspace}."${tableName}" """
    

    or escape the inner double quotes by additional ${...}:

    s"ALTER TABLE ${keyspace}.${'"'}${tableName}${'"'} "