1

I'm using Slick in my Scala/Play project.

In the config I've defined my database URL like this:

db.default.url="jdbc:mysql://localhost"

I have this simple method that runs a query. I need it to work on multiple databases(that cannot be defined in the conf), so I'm passing the database name as a parameter:

def getData(db: String): String = DB.withSession {
    sql"""SELECT myColumn FROM #$db.myTable LIMIT 1""".as[String].firstOption
}

This works just fine. But what I need is to convert the above example to this:

def getData(db: String): String = DB.withSession {
    sql"""SELECT myColumn FROM myTable LIMIT 1""".as[String].firstOption
}

Instead of just DB.withSession I need somehow to select the database name which is being passed with a param db. How can I do this?

UPDATE: I understand that something like Database.forURL("jdbc:mysql://localhost/dbName") could work, but then it also requires user name, password, etc, which already exists for a default connection DB, so it doesn't make much sense entering all those settings again.

Caballero
  • 11,546
  • 22
  • 103
  • 163

0 Answers0