我正在尝试在 Play2.0 中创建一个 PostgreSQL 触发器数据库演化脚本。 sql 代码相对简单,在 pgAdminIII 中运行良好:
CREATE OR REPLACE FUNCTION update_modified() RETURNS TRIGGER AS $$
BEGIN
NEW.modified = now();
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
但是,运行进化时出现错误:
ERROR: unterminated dollar-quoted
string at or near "$$ BEGIN NEW.modified = now()"
. SQL 代码似乎被截断了函数中遇到的第一个分号。我正在为 PostgreSQL 使用“9.1-901.jdbc4”JDBC 驱动程序。
更新:
Evolutions.scala中的代码(第 219+ 行)对
;
执行简单的拆分.框架本身似乎有问题:// Execute script
s.sql.split(";").map(_.trim).foreach {
case "" =>
case statement => execute(statement)
}
任何解决方案?
最佳答案
您可以尝试以下操作。
String sql = "CREATE OR REPLACE FUNCTION update_modified() RETURNS TRIGGER AS $$ " +
"BEGIN " +
"NEW.modified = now(); " +
"RETURN NEW; " +
"END; " +
"$$ LANGUAGE 'plpgsql';";
DataSource ds = getDataSource();
try {
Connection conn = ds.getConnection();
conn.setAutoCommit(true);
Statement st = conn.createStatement();
st.executeUpdate(sql);
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
希望这对你有用。
关于scala - 使用 JDBC 创建 PostgreSQL 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10460542/