JDBC显示语法错误,但查询在HeidiSQL中运行良好

时间:2019-08-01 23:32:35

标签: java mysql jdbc kotlin

我正在使用jasync将ktor连接到mysql。我的所有查询都运行正常,除了这一查询。即使删除了查询的一部分,它也会显示语法错误。

Mysql正在运行,并且可以很好地处理查询。我在HeidiSQL中运行相同的查询,它返回的结果很好,没有错误。

USE ${city.getString("DbName")};

SELECT
    m.id AS meter_id,
    m.meter_num AS meter_number,
    IF(
        l.split = 0,
        m.route_id,
        CONCAT(
            CAST(m.route_id AS CHAR),
            CAST(l.split AS CHAR)
        )
    ) AS route_id,
    m.sequence AS sequence_number,
    m.address,
    m.location,
    m.location_2,
    m.location_3,
    m.previous_read,
    m.account_num AS account_number,
    m.low_limit,
    m.high_limit,
    m.low_usage,
    m.high_usage,
    m.calc_usage AS calculated_usage,
    m.num_dials,
    CONCAT(h.Date, h.Time, LPAD(h.time_seconds, 2, '0')) as date_time,
    c.utility,
    c.acct_owner AS account_owner,
    c.MeterCode AS meter_code,
    m.msg,
    c.misc1,
    c.misc2,
    c.misc3,
    c.active,
    c.backward,
    mcd.always_require_photo AS require_photo
FROM
    datazeo.meters AS m
    INNER JOIN datazeo.routes AS rt ON rt.id = m.route_id
    INNER JOIN info.loads AS l ON l.RouteID = rt.route
    INNER JOIN custfile AS c ON c.IDnum = m.meter_num
    LEFT JOIN acsreads AS r ON r.IDnum = c.IDnum
    LEFT JOIN history AS h ON
        h.IDnum = c.IDnum
        AND h.Month = $month
    LEFT JOIN datazeo.meter_customer_data AS mcd ON mcd.meter_id = m.id
WHERE
    m.city_id = $cityId
    AND rt.city_id = $cityId
    AND l.CityID = "$paddedCityId"
    AND l.ReaderID = ${requestContext.user.id}
    AND r.IDnum IS NULL
    AND l.Recheck = 0
    AND (
        l.split = 0
        OR (
            m.sequence >= l.seq_start
            AND m.sequence <= l.seq_end
        )
)

使用HeidiSQL时,查询返回27列和153行。 但是当在jasync和JDBC中在kotlin中运行它时出现此错误...

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT
m.id AS meter_id,
m.meter_num AS meter_number,
IF(
    l.' at line 3"

1 个答案:

答案 0 :(得分:0)

找到了答案。无论出于何种原因,JDBC都不允许您在与select相同的请求中发送USE语句。

所以我将代码替换为这样

  tableDef: Array<any> = [
    {
      key: 'position',
      header: 'Position',
      className: 'something'
    },    {
      key: 'name',
      header: 'Element Name',
      className: 'anElement'
    },    {
      key: 'weight',
      header: 'Weight',
      className: 'number'
    },    {
      key: 'symbol',
      header: 'A long text to display in header',
      className: 'something'
    },
  ]

只需在表之前指定数据库,即可正常工作。