简体   繁体   English

我如何将表导入另一个类(/ object ??),以便可以在另一个类/对象中对它运行查询? [slick 3.0] [scala]

[英]How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala]

I have two tables defined in a 我在一个表中定义了两个表

 class Patients(tag: Tag) extends Table[(String, String, Int, String)](tag, "Patientss") {
    def PID = column[String]("Patient Id", O.PrimaryKey)

    def Gender = column[String]("Gender")

    def Age = column[Int]("Age")

    def Ethnicity = column[String]("Ethnicity")

    def * = (PID, Gender, Age, Ethnicity)
  }

  val patientsss = TableQuery[Patients]

  class DrugEffect(tag: Tag) extends Table[(String, String, Double)](tag, "DrugEffectss") {

    def DrugID = column[String]("Drug ID", O.PrimaryKey)

    def PatientID = column[String]("Patient_ID")

    def DrugEffectssss = column[Double]("Drug Effect")

    def * = (DrugID, PatientID, DrugEffectssss)

    def Patient = foreignKey("Patient_FK", PatientID, patientsss)(_.PID)
  }

  val d_effects = TableQuery[DrugEffect]

I fill in the tables in this particular object/class as well. 我也填写该特定对象/类中的表。

I was wondering how I could call the filled in tables in another object so I can access both DrugEffect and Patients as a class, and then run queries on the table itself? 我不知道我怎么会叫填写表中的另一个对象,这样我可以同时访问DrugEffectPatients为一类,然后再运行查询的表本身?

I hope I'm making myself clear, I don't really have a clue about what I'm doing 我希望我能把自己弄清楚,我对自己在做什么一无所知

What I mean by running queries is something like this: 我通过运行查询的意思是这样的:

val q1 = for {
    c <- patientsss if (c.Age === 20 && c.Gender === "F")
    s <- d_effects if (s.DrugEffectssss > 10.0)
  } yield (c.PID, s.DrugID)

but in an object defined in a different file 但是在另一个文件中定义的对象中

You need the DB API and the table in the separate class. 您需要在单独的类中使用DB API和表。 You can do something like: 您可以执行以下操作:

import tables.Tables

class SeparateClass extends HasDatabaseConfig[JdbcProfile] {
  val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)

  import driver.api._

  def get(id: Long) = {
    db.run(Tables.DrugEffect.d_effects.filter(_.id === id).result)
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将类对象用于其他对象? - How can I use a class object into other? 如何将文件名字符串传递给另一个 Class 以便它在该 class 中创建文件 object? - How can I pass a filename String to another Class so that it creates a File object in that class? 我如何引用另一个类的对象 - how can I refer to an object of another class 我如何在另一个类中创建一个类的对象 - how can I create object of a class in another class 在 Java 中,当我可以导入超类时,为什么我无法将子类导入另一个类? - In Java why would I be unable to import a sub-class into another class when I can import the superclass? 我可以使用Hibernate连接不同的数据库并从表中导入数据吗? 没有预定义的对象类 - Can I connect with different databases and import data from tables using Hibernate? without a predefined object class How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java - How can I copy existing class object to other class new class object without creating copy of original object(in memory) in Java 如何使用Scala类作为跨类加载器的共享根对象? - How can I use a Scala class as the shared root object across class loaders? 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another 我如何在另一个类中使用一个文件对象 - how can i use one file object in another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM