Anorm 将整数和字符串值保存到 table
Anorm save integer and string values to table
我有以下代码块,创建它是为了插入对调查问题的答复:
def saveResponse(qId:String,userEmail:String, response:String) = {
DB.withConnection{
implicit c =>
val notAString = qId.toInt
val id = SQL(s"INSERT INTO responses(response,useremail,questionid) values ({userResp},{eMail},{quesId})")
.on('userResp -> s"$response", 'eMail -> s"$userEmail",'quesId -> s"$notAString")
.executeInsert()
当我运行这段代码时,我得到以下错误:
[PSQLException: ERROR: column "questionid" is of type integer but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
我已经尝试了几种不同的转换方式(valueOf、toInt 等),但始终出现相同的错误。我玩过不同的 resultSetParsers 但我不完全确定如何使用它们,因为文档似乎有点稀疏。当我将此查询单独输入到 psql 控制台时,它似乎可以正常工作,例如:
insert into responses(response,useremail,questionid) values ('true','qbert@bacon.com',2);
我不确定我的异常代码做错了什么以及为什么我无法在同一个插入块中处理字符串和整数。当我删除 int 部分时,它确实只插入了电子邮件和问题回复,但这毫无用处,因为我没有问题 ID 可以引用它。
这个:
'quesId -> s"$notAString"
需要:
'quesId -> notAString
将其作为整数传递。
我有以下代码块,创建它是为了插入对调查问题的答复:
def saveResponse(qId:String,userEmail:String, response:String) = {
DB.withConnection{
implicit c =>
val notAString = qId.toInt
val id = SQL(s"INSERT INTO responses(response,useremail,questionid) values ({userResp},{eMail},{quesId})")
.on('userResp -> s"$response", 'eMail -> s"$userEmail",'quesId -> s"$notAString")
.executeInsert()
当我运行这段代码时,我得到以下错误:
[PSQLException: ERROR: column "questionid" is of type integer but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
我已经尝试了几种不同的转换方式(valueOf、toInt 等),但始终出现相同的错误。我玩过不同的 resultSetParsers 但我不完全确定如何使用它们,因为文档似乎有点稀疏。当我将此查询单独输入到 psql 控制台时,它似乎可以正常工作,例如:
insert into responses(response,useremail,questionid) values ('true','qbert@bacon.com',2);
我不确定我的异常代码做错了什么以及为什么我无法在同一个插入块中处理字符串和整数。当我删除 int 部分时,它确实只插入了电子邮件和问题回复,但这毫无用处,因为我没有问题 ID 可以引用它。
这个:
'quesId -> s"$notAString"
需要:
'quesId -> notAString
将其作为整数传递。