PostgreSQL 提示:您需要重写或转换表达式。列“state”是类型状态,但表达式是字符类型可变的
2022-09-01 17:31:16
我正在尝试使用java创建一个SQL语句。问题是我正在使用
stmt.setString(9, ev.getState().status());
对于我正在尝试插入到状态类型的SQL列中的变量
CREATE TYPE STATUS AS ENUM ('APPROVED', 'CLOSED','STARTED', 'WAITING');
它给我抛出了一个例外
column "state" is of type status but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
是我犯了错误,还是实际上需要将值转换为 sql?如果是,在这种情况下如何施法?
完整声明:
PreparedStatement stmt = conn.prepareStatement("INSERT INTO Event (EventNum, EventName, startHour, endHour, startMin, endMin, startDate, endDate, State, depName) VALUES (?, ?, ?, ?, ?, ?, ?::date, ?::date, ?, ?)");
stmt.setInt(1, ev.getEventNum());
stmt.setString(2, ev.getName());
stmt.setInt(3, ev.getStartHour());
stmt.setInt(4, ev.getEndHour());
stmt.setInt(5, ev.getStartMinute());
stmt.setInt(6, ev.getEndMinute());
stmt.setString(7, ev.getStartYear() + "-" + ev.getStartMonth() + "-" + ev.getStartDate());
stmt.setString(8, ev.getEndYear() + "-" + ev.getEndMonth() + "-" + ev.getEndDate());
stmt.setString(9, ev.getState().status());
stmt.setString(10, ev.getDepartment());
stmt.executeUpdate();