我需要在PostgreSQL表中插入带有随机数的多行(大约1个。)。此代码将随机数插入一列。我怎么能让它同时向所有列插入随机数据?
import slick.driver.PostgresDriver.simple ._
object Main {
class Users(tag: Tag) extends Table[(Int, String, String)](tag, "users") {
def id = column[Int]("id")
def username = column[String]("username")
def miestas = column[String]("miestas")
def * = (id, username, miestas)
}
def main(args: Array[String]): Unit = {
val connectionUrl = "jdbc:postgresql://localhost/test?user=postgres&password=kurmis"
Database.forURL(connectionUrl, driver = "org.postgresql.Driver") withSession {
implicit session =>
val users = TableQuery[Users]
for (a <- 1 to 10) {
val r = scala.util.Random
users.map(_.username) += r.nextInt.toString
}
//Insert
val r = scala.util.Random
users.map(_.username) += r.nextInt.toString
println("pabandom")
}
}
}
答案 0 :(得分:0)
The documentation有这个例子:
people.map(p => (p.name, p.age, p.addressId)) += ("M Odersky",12345,1)
等效的SQL:
insert into PERSON (NAME, AGE, ADDRESS_ID) values ('M Odersky', 12345, 1)
所以在你的情况下你可能会写这样的东西:
users.map(u => (u.username, u.miestas)) += (r.nextInt.toString, r.nextInt.toString)