James
James

Reputation: 4052

Very simple Slick query, get a single value

I'm trying to introduce slick into my code to replace some existing jdbc code.

First of all I'd like to use a scala worksheet to run a really simple query, I want to pass in an integer id, and get back a string uuid. This is the simplest method in the whole codebase.

As I understand I need to make a connection to the database, setup an action, and then run the action. I have the following code:



val db = Database.forURL("jdbc:mysql://mysql-dev.${URL}/${DB}?autoReconnect=true&characterEncoding=UTF-8",
  driver = "com.mysql.jdbc.Driver", user = "${user}",password= "${pass}")

val getUUID = sql"""SELECT ${UUIDFIELD} from users u WHERE u.${IDFIELD} = ${Id}""".as[String]

val uuid:String = db.run(getUUID)
println(uuid)

I'm pretty sure I don't have the driver setup correctly in the Database.forURL call, but also the worksheet is complaining that the result of db.run is not a string. How do I get to the string UUID value?

Upvotes: 0

Views: 344

Answers (1)

John Mullins
John Mullins

Reputation: 1101

The db.run method returns a Future[_] type. You should use Await for getting result from it.

val db = Database.forURL("jdbc:mysql://mysql-dev.${URL}/${DB}?autoReconnect=true&characterEncoding=UTF-8",
  driver = "com.mysql.jdbc.Driver", user = "${user}",password= "${pass}")

val getUUID = sql"""SELECT ${UUIDFIELD} from users u WHERE u.${IDFIELD} = ${Id}""".as[String]

val uuidFuture:Future[String] = db.run(getUUID)

import scala.concurrent._
import scala.concurrent.duration._

val uuid:String = Await.result(uuidFuture, Duration.Inf)
println(uuid)

Upvotes: 2

Related Questions