光滑的表格列表

时间:2016-02-01 15:21:50

标签: slick

我想从我通过JDBC连接的数据库中打印一个表列表,但是无法弄清楚如何执行此操作。我尝试过使用MTable.getTablesdefaultTables,谢谢

import slick.driver.H2Driver.api._
import scala.concurrent._
import com.typesafe.config.{ ConfigFactory, Config }

object SlickTest {

  def main(args: Array[String]): Unit = {

    """
      |mydb = {
      |  driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
      |  slickDriver = com.typesafe.slick.driver.ms.SQLServerDriver
      |  url="jdbc:sqlserver://#############"
      |  properties = {
      |    databaseName = "######"
      |    user = "#######"
      |    password = "########"
      |  }
      |  numThreads = 10
      |}
    """.stripMargin


    val db = Database.forConfig("mydb")


    ???

  }

}

1 个答案:

答案 0 :(得分:1)

有很多不同的方法可以做到这一点,但是按照你在问题中的代码布局,这适用于Slick 3.1(和使用freeslick配置文件,因为我没有光滑的扩展SQLServer LIB)。

import com.typesafe.config.ConfigFactory
import slick.backend.DatabaseConfig
import slick.driver.JdbcProfile
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext}


object SlickTest {

  def main(args: Array[String]): Unit = {
    val config = """
    |mydb = {
    |
    |  driver = "freeslick.MSJDBCSQLServerProfile$"
    |  db {
    |    driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
    |    url="jdbc:sqlserver://localhost:2008"
    |    properties = {
    |      databaseName = "freeslicktest"
    |      user = "sa"
    |      password = "FreeSlick"
    |    }
    |  }
    |}
    """.stripMargin
    val dbConfig = DatabaseConfig.forConfig[JdbcProfile]("mydb", ConfigFactory.parseString(config))
    import ExecutionContext.Implicits.global
    Await.result(dbConfig.db.run(dbConfig.driver.defaultTables), Duration.Inf).foreach(println)
  }
}

为数据库生成此结果,其中包含一个名为USERS的表。

*** (c.z.hikari.HikariDataSource) HikariCP pool mydb.db is starting.
MTable(MQName(freeslicktest.dbo.USERS),TABLE,null,None,None,None)

Process finished with exit code 0