0
因此,我使用Slick的SQLite数据库,并且我想从中添加和删除表。 这是我现在有:使用Slick和Scala将表添加到SQLite数据库
这里是数据库元素类:
class Data(tag: Tag)
extends Table[(Int, String)](tag, "myDB") {
// This is the primary key column:
def id = column[Int]("ID", O.PrimaryKey)
def name = column[String]("NAME")
// Every table needs a * projection with the same type as the table's type parameter
def * : ProvenShape[(Int, String)] = (id, name)
}
我需要能够创建使用上述类的多个表。这样的事情:
def addTable(name:String){
db withSession { implicit session =>
val newTable = TableQuery[Data]
newTable.ddl.create
}
}
问题是我不能创建新表,因为一个已经存在的名称为“myDB”。我试图在类的数据添加一个参数为表的名称,像这样:
class Data(tag: Tag,tableName:String)
但我不能在所有创建一个表,并得到了一个错误
unspecified value parameter tableName
又如何我可以从给定表名的数据库中查询特定的表吗? 我试图实现这一点使用地图与表名指向一个表,但它不工作,因为该地图不保存在任何地方,每当程序启动时重置。
这是我有什么查表:
def getDataFromTable(tableName:String)
{
var res = ""
db withSession { implicit session =>
tables(tableName) foreach{
case (id,name)=>
res += id + " " + name + " "
}
}
res
}
任何帮助表示赞赏!
谢谢!