提问者:小点点

错误的SQL语法,当我给一个时间戳参数


在我的Java应用程序中,我曾经有一个sql查询,如下所示:

INSERT INTO "KPI_MEASURE" (
        id,
        created_at,
        kpi_project_id,
        kpi_frequency_id,
        kpi_metric_id,
        branch,
        value
    )
SELECT
    nextval('"KPI_MEASURE_ID_seq"'::regclass),
    now(),
    kpi_measure.kpi_project_id,
    kpi_measure.kpi_frequency_id,
    kpi_measure.kpi_metric_id ,
    kpi_measure.branch ,
    sum(kpi_measure.value)
FROM "KPI_MEASURE" kpi_measure
    INNER JOIN "KPI_METRIC" kpi_metric                      ON kpi_measure.kpi_metric_id = kpi_metric.id
    INNER JOIN "KPI_PROJECT" kpi_project                    ON kpi_measure.kpi_project_id = kpi_project.id
    INNER JOIN "KPI_AGGREGATION_PROJECT" kpi_agg_project    ON kpi_project.name = kpi_agg_project.child_project_name
    WHERE kpi_metric.aggregated = false
GROUP BY kpi_measure.branch, kpi_measure.kpi_metric_id, kpi_measure.kpi_project_id, kpi_project.name, kpi_measure.kpi_frequency_id;

我用jdbcTemboard. update执行了那个sql,它工作了。
但是最近我通过一个参数更改了值now():today_date,我用我的代码给出了这个参数:

Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(TODAY_DATE, today); // TODAY_DATE = today_date

jdbcTemplate.update(sql, parameters);

但是现在,它会产生一个错误:

BadSqlGrammarException:准备状态回调;坏SQL语法

嵌套异常是org. postgresql.util.PSQLException:未安装hstore扩展。

我不明白为什么这是一个错误,因为这是我被告知要做的,当我们想要使用jdbc查询添加参数时。

列数据类型是没有时区的时间戳,而今天的变量是String类型。
我想,我应该使用Timestamp,所以我改变了我的代码:

Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(TODAY_DATE, new Timestamp(System.currentTimeMillis())); // TODAY_DATE = today_date

jdbcTemplate.update(sql, parameters);

现在我只有错误

"PSQLException:未安装hstore扩展。"

也许,我需要添加一个hstore,但我似乎不明白为什么。


共1个答案

匿名用户

您使用带有命名参数的JdbcTenmplate,但JdbcTemplate仅支持定位参数(?)。将JdbcTemplate替换为NamedParameterJdbcTemplate或将您的查询重写为:

INSERT INTO "KPI_MEASURE" (
        id,
        created_at,
        kpi_project_id,
        kpi_frequency_id,
        kpi_metric_id,
        branch,
        value
    )
SELECT
    nextval('"KPI_MEASURE_ID_seq"'::regclass),
    ?,
    kpi_measure.kpi_project_id,
    kpi_measure.kpi_frequency_id,
    kpi_measure.kpi_metric_id ,
    kpi_measure.branch ,
    sum(kpi_measure.value)
FROM "KPI_MEASURE" kpi_measure
    INNER JOIN "KPI_METRIC" kpi_metric                      ON kpi_measure.kpi_metric_id = kpi_metric.id
    INNER JOIN "KPI_PROJECT" kpi_project                    ON kpi_measure.kpi_project_id = kpi_project.id
    INNER JOIN "KPI_AGGREGATION_PROJECT" kpi_agg_project    ON kpi_project.name = kpi_agg_project.child_project_name
    WHERE kpi_metric.aggregated = false
GROUP BY kpi_measure.branch, kpi_measure.kpi_metric_id, kpi_measure.kpi_project_id, kpi_project.name, kpi_measure.kpi_frequency_id;

并调用jdbcTemboard. update(sql,new Timestamp(System.current tTimeMillis()));