Skip to content
Advertisement

How to send plain SQL queries (and retrieve results) using scala slick 3

I am trying to make a class that has methods that can send and get data to an SQLite db using plain sql queries. This unfortunately does not work. I do not want to use the withSession implicit parts.

import slick.driver.SQLiteDriver.api._
import slick.lifted.TableQuery
import slick.jdbc.JdbcBackend.Database;
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import ExecutionContext.Implicits.global

    class DBops {
  val db = Database.forURL("jdbc:sqlite:S:/testing/testdb.sql",driver = "org.sqlite.JDBC")

def getData(TableName: String):Future[(Int,Double,String)]={
    db.run(sql"""select * from $TableName """.as[(Int,Double,String)])

}

}

The following error is thrown:

type mismatch; found : slick.profile.SqlStreamingAction[Vector[(Int, Double, String)],(Int, Double, String),slick.dbio.Effect] required: slick.dbio.DBIOAction[(Int, Double, String),slick.dbio.NoStream,Nothing] DBops.scala

Advertisement

Answer

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)])
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement