Slick 3 for scala更新查询无法正常运行

时间:2016-01-26 06:48:19

标签: slick slick-3.0

首先,我想声明我是新手,并且正在使用3.1.1版本。我一直在阅读手册,但我无法让我的查询工作。我的连接字符串出现问题或者我的Slick代码有问题。我从http://slick.typesafe.com/doc/3.1.1/database.html这里得到了我的配置,我的更新示例来自页面http://slick.typesafe.com/doc/3.1.1/queries.html的底部。好的,这是我的代码

应用配置

mydb= {
  dataSourceClass = org.postgresql.ds.PGSimpleDataSource
  properties = {
    databaseName = "Jsmith"
    user = "postgres"
    password = "unique"
  }
  numThreads = 10
}

我的控制器 - 调用数据库表 - 关系

package controllers
import play.api.mvc._
import slick.driver.PostgresDriver.api._

class Application extends Controller {

  class relations(tag: Tag) extends Table[(Int,Int,Int)](tag, "relations") {
    def id = column[Int]("id", O.PrimaryKey)
    def me = column[Int]("me")
    def following = column[Int]("following")
    def * = (id,me,following)

  }

  val profiles = TableQuery[relations]

  val db = Database.forConfig("mydb")


  try {
    // ...
  } finally db.close()




  def index = Action {

    val q = for { p <- profiles if p.id === 2 } yield p.following
    val updateAction = q.update(322)
    val invoker = q.updateStatement
    Ok()
  }

}

上面的代码有什么问题?我有一个使用普通JDBC的独立项目,这个配置非常适合它

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/Jsmith"
db.default.user="postgres"
db.default.password="unique"

2 个答案:

答案 0 :(得分:2)

你还没有开始行动。 db.run(updateAction)分别对数据库执行查询(未经测试):

def index = Action.async {
  val q = for { p <- profiles if p.id === 2 } yield p.following
  val updateAction = q.update(322)

  val db = Database.forConfig("mydb")
  db.run(updateAction).map(_ => Ok())
}

db.run()会返回Future,最终会完成。然后将其简单地映射到游戏中的Result

另一方面,

q.updateStatement只生成一个sql语句。这在调试时很有用。

答案 1 :(得分:0)

从我的项目中查看代码:

def updateStatus(username: String, password: String, status: Boolean): Future[Boolean] = {
  db.run(
    (for {
      user <- Users if user.username === username
    } yield {
      user
    }).map(_.online).update(status)
  }