gpt4 book ai didi

database - H2 内存数据库,[JdbcSQLException : Table "USERINFO" not found; SQL statement:

转载 作者:搜寻专家 更新时间:2023-10-30 20:39:03 26 4
gpt4 key购买 nike

我在内存数据库中使用 H2 和 play 框架 我是初学者所以我决定制作一个小的登录注册应用程序来开始使用 scala 并玩我决定在内存数据库中使用 H2 但它抛出 Table未找到异常我在 sql 文件中编写了一个脚本并播放要求我在启动应用程序时应用此脚本但是当我尝试向其中插入数据时它抛出异常,异常是

 [JdbcSQLException: Table "USERINFO" not found; SQL statement:
insert into USERINFO (fname,lname,email,userName,pwd,age,choice,gender) values (?,?,?,?,?,?,?,?) [42102-175]]

这是sql文件的脚本

# --- !Ups
CREATE TABLE USERINFO(
fname varchar(255) NOT NULL,
lname varchar(255) NOT NULL,
email varchar(255) NOT NULL,
userName varchar(255) NOT NULL,
pwd varchar(255) NOT NULL,
age Int NOT NULL,
choice varchar(255) NOT NULL,
gender varchar(255) NOT NULL
);

DROP TABLE USERINFO;

这是 application.conf 的一部分

 db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
db.default.user=sa
db.default.password=""
db_close_delay=-1

这里是application.scala的代码

package controllers

import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import views.html.defaultpages.badRequest
import play.api.data.validation.Constraints._

import models.User



object Application extends Controller {


val RegisterForm = Form(
mapping(

"fname" -> nonEmptyText(1, 20),
"lname" -> nonEmptyText(1, 20),
"email" -> email,
"userName" -> nonEmptyText(1, 20),
"password" -> nonEmptyText(1, 20),
"age" -> number,
"choice" -> text,
"gender" -> text

)
(User.apply)(User.unapply)
verifying("Ag should be greater then or eual to 18",model=> model.age match
{
case (age) => age>=18

})
)
def index = Action {
Ok(views.html.index(RegisterForm))
}

def register =Action {implicit request =>

RegisterForm.bindFromRequest().fold(
hasErrors => BadRequest(views.html.index(hasErrors))
,
success => {

val result = User.save(success)
println(s"INSERT succeeded, id = $result")
Redirect(routes.Application.index)
}
)
}

}

这是User.scala的代码

package models
import anorm._
import play.api.db.DB
import anorm.SqlParser._
import play.api.Play.current

case class User (

fname:String,
lname:String,
email:String,
userName:String,
password:String,
age:Int,
choice:String,
gender:String

)
object User{

val userinfo = {
get[String]("fname") ~
get[String]("lname") ~
get[String]("email") ~
get[String]("userName") ~
get[String]("pwd") ~
get[Int]("age") ~
get[String]("choice") ~
get[String]("gender") map {
case fname ~ lname ~email~ userName ~ password ~age~ choice~gender =>
User(fname , lname ,email, userName , password ,age, choice,gender)
}
}
def save(ud:User):Option[Long]= {
val id :Option[Long] = DB.withConnection {implicit c =>
SQL ("insert into USERINFO (fname,lname,email,userName,pwd,age,choice,gender) values ({fname},{lname},{email},{userName},{password},{age},{choice},{gender})")
.on('fname -> ud.fname, 'lname ->ud.lname ,'email ->ud.email, 'userName->ud.userName , 'password->ud.password ,'age->ud.age, 'choice->ud.choice,'gender->ud.gender)
.executeInsert()
}
id
}

/* def save(ud:User)= {
DB.withConnection {implicit c =>
SQL ("insert into USERINFO (fname,lname,email,userName,pwd,age,choice,gender) values ({fname},{lname},{email},{userName},{password},{age},{choice},{gender})")
.on('fname -> ud.fname, 'lname ->ud.lname ,'email ->ud.email, 'userName->ud.userName , 'password->ud.password ,'age->ud.age, 'choice->ud.choice,'gender->ud.gender)
.executeUpdate()
}

}
*/
}

我尝试了 executeUpdateexecuteInsert 但没有任何改变请帮助我哪里做错了

最佳答案

1.

问题很可能是 Play! Framework在查询中引用了标识符名称(表名、列名),所以在'create table'语句中也需要引用表名:

    CREATE TABLE "USERINFO"(
"fname" varchar(255) NOT NULL,
"lname" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
"userName" varchar(255) NOT NULL,
"pwd" varchar(255) NOT NULL,
"age" Int NOT NULL,
"choice" varchar(255) NOT NULL,
"gender" varchar(255) NOT NULL
);

2.

如果没有任何帮助。还有一种方法。我阅读了迁移文档,必须将 libraryDependencies += evolutions 应用于 build.sbt。

这些不是必需的,它们会自动应用进化(不显示数据库“默认”需要进化!):

applyEvolutions.db=true
applyEvolutions.default=true
applyDownEvolutions.default=true

关于database - H2 内存数据库,[JdbcSQLException : Table "USERINFO" not found; SQL statement:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28193656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com