2013-07-24 37 views
1

排序的油滑的n00b ...我试图建立一个插入语句,但发现我没有插入任何东西。油滑的斯卡拉没有插入任何值

def newUser(userName: String, email: Option[String], password: String = null, removed: Option[Int] = null) = SlickInit.dbMaster withSession { 

    val creation = Option(new java.sql.Timestamp(new java.util.Date().getTime())) 
    val last = new java.sql.Timestamp(new java.util.Date().getTime()) 

    printf("REACHED 1. creation: " + creation + "\n last: " + last) 
    println("\nusername: " + userName + "\n email: " + email) 
    println("maxId: " + maxId) 

    val invoker = Users.insertInvoker 

    (Users.userId ~ Users.userName ~ Users.email ~ Users.userPassword ~ Users.creationDate ~ Users.lastLoginDate ~ Users.removed).insert(maxId, userName, email, password, creation, last, removed) 

    val statement = Users.insertStatement 
    println("REACHED 2. \n\nStatement: " + statement) 
} 

达到了1张打印(所需值)时,POST请求发出,但没有达到2.我敢肯定,那里有我的INSERT语句做错事,但我不知道是什么。 (另外,很明显,当我查询数据库时,插入的值不会返回。)有没有人对此有所了解?

编辑,这里是我的用户表的定义:

object Users extends Table[(Int, String, Option[String], String, Option[Timestamp], Timestamp, Option[Int])]("APUsers") { 

    def userId   = column[Int]("UserId", O.PrimaryKey, O.AutoInc) 
    def userName  = column[String]("UserName") 
    def email   = column[Option[String]]("Email", O.Nullable) 
    def userPassword = column[String]("UserPassword") 
    def creationDate = column[Option[Timestamp]]("CreationDate", O.Nullable) 
    def lastLoginDate = column[Timestamp]("LastLoginDate") 
    def removed   = column[Option[Int]]("removed", O.Nullable) 

    def * = userId ~ userName ~ email ~ userPassword ~ creationDate ~ lastLoginDate ~ removed 
} 
+0

你能还发布用户对象定义? –

+0

是否看到抛出异常?你在使用内存数据库吗? – pedrofurla

回答

4

在用户对象定义,您需要更改此设置:

object Users extends Table[(Int, String, Option[String], String, Option[Timestamp], Timestamp, Option[Int])]("APUsers") { 

这样:

object Users extends Table[(Option[Int], String, Option[String], String, Option[Timestamp], Timestamp, Option[Int])]("APUsers") { 

因为由DBMS(O.AutoInc)分配id。在这里看到的例子:http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#mapped-tables

然后,你需要改变这一点:

def * = userId ~ userName ~ email ~ userPassword ~ creationDate ~ 
     lastLoginDate ~ removed 

这样:

def * = userId.? ~ userName ~ email.? ~ userPassword ~ creationDate.? ~ 
     lastLoginDate ~ removed.? 

,因为它们被定义为Option。在这里看到:http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#mapped-tables

newUser插入线应该是,从这样的:

(Users.userId ~ Users.userName ~ Users.email ~ Users.userPassword ~ 
Users.creationDate ~ Users.lastLoginDate ~ Users.removed) 

(Users.userName ~ Users.email.? ~ Users.userPassword ~ Users.creationDate.? ~ 
Users.lastLoginDate ~ Users.removed.?) 
.insert(userName, email, password, creation, last, removed) 

没有userId,因为它会由DBMS分配。在这里看到的例子:http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#inserting

既然你不数据库接受空,我建议改变这一点:

def newUser(userName: String, email: Option[String], password: String = null, removed: Option[Int] = null) 

这样:

def newUser(userName: String, email: Option[String], password: String, removed: Option[Int] = null) 

,并检查密码不为空。

按pedrofurla建议你可以添加以下到Users对象:

def forInsert = Users.userName ~ Users.email.? ~ Users.userPassword ~ 
       Users.creationDate.? ~ Users.lastLoginDate ~ Users.removed.? 

,使线条更具可读性:

Users.forInsert.insert(userName, email, password, creation, last, removed) 

http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#inserting

+0

感谢David的建议。不幸的是,Eclipse抛出错误: ' - 方法插入的参数太多:(值:(Int,String,Option [Option [String]],String,Option [Option [java.sql.Timestamp]], \t java.sql.Timestamp,Option [Option [Int]]))(隐式会话:scala.slick.session.Session)Int' –

+0

我建议使用投影'*'。 'Users。*。insert ...' – pedrofurla

+1

@pedrofurla我在回答中加了你的建议。 –