How to send plain SQL queries (and retrieve results) using scala slick 3
You can use sql"..."
with any String
content using #$tableName
:
db.run(sql"SELECT * FROM #$tableName".as[(Int, Double, String)])
Please remember: don't ever take the tableName
as a user input - otherwise, there is a great risk for SQL injection. The normal $value
solves these problems for you.
Read Slick manual (http://slick.typesafe.com/doc/3.0.0/sql.html#splicing-literal-values)
... sometimes you need to splice literal values directly into the statement, for example to abstract over table names ... You can use #$ instead of $ in all interpolators for this purpose ...
I realized I was missing a Seq in the return type:
def getData(TableName: String): Future[Seq[(Int,Double,String)]] = {
db.run(sql"""SELECT * FROM $TableName """.as[(Int, Double, String)])
}