[英]Adding tables to SQLite database using Slick and Scala
所以我有使用Slick的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”的表。 我试图像这样在类Data中为Table的名称添加一个参数:
class Data(tag: Tag,tableName:String)
但是后来我根本无法创建表并出现错误
unspecified value parameter tableName
给定表名,如何从数据库中查询特定表? 我尝试使用映射名指向一个表的Map来实现此功能,但是它不起作用,因为Map不会保存在任何地方,并且每次程序启动时都会被重置。
这是我查询表的内容:
def getDataFromTable(tableName:String)
{
var res = ""
db withSession { implicit session =>
tables(tableName) foreach{
case (id,name)=>
res += id + " " + name + " "
}
}
res
}
任何帮助表示赞赏!
谢谢!
定义
class Data(tag: Tag, tableName: String)
extends Table[(Int, String)](tag, tableName){
...
用法
(new TableQuery(Data(_,"table_1"))).ddl.create
(new TableQuery(Data(_,"table_2"))).ddl.create
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.