mysql - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column 'RECEPTORS.r_name' in 'field list'

标签 mysql jdbc playframework sbt multiple-tables

我正在使用 play 2.3.8 框架创建 API 并访问 mariaDB。当我在 mariaDB 控制台上运行查询时,它工作正常,但是当我从游戏中运行它时,我收到错误消息,指出字段 RECEPTORS.r_name 不可用,这是不正确的。

我的代码是

package models.dao

import anorm._
import models.Profile
import play.api.db.DB
import play.api.Play.current

object ProfileDAO {

def index(r_name: String): List[Profile] = {
  DB.withConnection { implicit c =>
    val results = SQL(
      """
        | SELECT `RECEPTORS.r_name`,`RECEPTORS.pdbCode`, `LIGANDS.l_id`, `LIGANDS.l_score` 
        | FROM `RECEPTORS`
        | INNER JOIN `LIGANDS`
        | WHERE `RECEPTORS.r_name`={r_name};
      """.stripMargin).on(
        "r_name" -> r_name
      ).apply()

    results.map { row =>
      Profile(row[String]("r_name"), row[String]("pdbCode"),row[String]("l_id"),row[Double]("l_score"))
    }.force.toList
  }
 }

}

我在 mariaDB 控制台上运行的查询是

SELECT RECEPTORS.r_name, pdbCode, l_id, l_score FROM RECEPTORS INNER JOIN LIGANDS WHERE RECEPTORS.r_name="receptor";

用Play 2.3.8运行报错如下

laeeq@optiplex:~/Desktop/Backup/Project5/cpvsAPI$ sbt -jvm-debug 9999 run Listening for transport dt_socket at address: 9999 [info] Loading project definition from /home/laeeq/Desktop/Backup/Project5/cpvsAPI/project [info] Set current project to cpvsAPI (in build file:/home/laeeq/Desktop/Backup/Project5/cpvsAPI/) [info] Updating {file:/home/laeeq/Desktop/Backup/Project5/cpvsAPI/}root... [info] Resolving jline#jline;2.11 ... [info] Done updating.

--- (Running the application, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

SLF4J: The following set of substitute loggers may have been accessed SLF4J: during the initialization phase. Logging calls during this SLF4J: phase were not honored. However, subsequent logging calls to these SLF4J: loggers will work as normally expected. SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger SLF4J: org.webjars.WebJarExtractor [info] Compiling 1 Scala source to /home/laeeq/Desktop/Backup/Project5/cpvsAPI/target/scala-2.11/classes... [info] play - database [default] connected at jdbc:mysql://localhost:3306/db_profile [info] play - Application started (Dev) [error] application -

! @766oc7b8l - Internal server error, for (GET) [/profiles/receptor] ->

play.api.Application$$anon$1: Execution exception[[MySQLSyntaxErrorException: Unknown column 'RECEPTORS.r_name' in 'field list']] at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8] at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8] at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:205) [play_2.11-2.3.8.jar:2.3.8] at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:202) [play_2.11-2.3.8.jar:2.3.8] at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.1.jar:na] Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'RECEPTORS.r_name' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_151] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_151] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_151] at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_151] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.18.jar:na]

最佳答案

您需要单独引用表格和列,因此使用:

`RECEPTORS`.`r_name`

否则 MySQL 认为您正在尝试引用某个隐式表中名称为 RECEPTORS.r_name 的列。

您需要对所有(引用的)列引用执行此操作。特别是在这种情况下,引号似乎是不必要的,因此您也可以只使用不带反引号的 RECEPTORS.r_name

关于mysql - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column 'RECEPTORS.r_name' in 'field list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47591698/

相关文章:

mysql - 更新查询需要太长时间才能更新mysql中的大量数据

java - PostgreSQL JDBCPreparedStatement的setBytes更改参数值

java - 未处理的异常类型NoSuchFieldException(java反射)

php - 使用 MongoDB 安装 WordPress

java.sql.SQLException : No suitable driver found for jdbc:mysql://localhost:3306 异常

java - 如何在Java应用程序中将数据写入CSV文件

java - jsp中过滤记录的sql查询

scala - 执行 play Future 查询时,scala 变量的值不会改变。为什么?

angularjs - Chrome 上的 POST 请求 "stall"

MySQL 大量记录的性能 - 分区?