无法使用callable语句或prepared语句调用PostgreSQL函数

无法使用callable语句或prepared语句调用PostgreSQL函数,postgresql,scala,function,stored-procedures,Postgresql,Scala,Function,Stored Procedures,我已经创建了一个PostgreSQL函数,该函数在后端进行了测试,并按预期工作。然而,当我试图通过Scala模块调用它时,它说该函数不存在 功能: create or replace function testing.compareData(ab integer, b json, tablename varchar) RETURNS void as $$ DECLARE actualTableName varchar := tablename; histTableName varc

我已经创建了一个PostgreSQL函数,该函数在后端进行了测试,并按预期工作。然而,当我试图通过Scala模块调用它时,它说该函数不存在

功能

create or replace function testing.compareData(ab integer, b json, tablename varchar) RETURNS void as $$
  DECLARE 
  actualTableName varchar := tablename;
  histTableName varchar:= actualTableName ||'_hist';
  job_id Integer:=0;
begin --<<<< HERE
  set search_path to testing; -- Set the schema name
  execute 'SELECT id FROM '||actualTableName||' WHERE id =$1' into job_id using ab;
  -- if there is data for id in the table then perform below operations
  if job_id is not null then
      execute FORMAT('INSERT INTO %I select * from %I where id = $1',histTableName,actualTableName) USING ab;
      execute FORMAT('DELETE FROM %I where id = $1',actualTableName) USING ab;
      EXECUTE FORMAT('INSERT INTO %I values($1,$2)',actualTableName) USING ab,b;
  -- if id is not present then create a new record in the actualTable
  ELSE    
      EXECUTE FORMAT('INSERT INTO %I values($1,$2)',actualTableName) USING ab,b;
  END IF;

END; --<<<< END HERE
$$ LANGUAGE plpgsql;
准备好的声明
方式:

def callDataCompareAndInsertFunction(message: String, id: Integer, resourceType: String): Unit = {
    val connectionUrl: String = ReadingConfig.postgreDBDetails().get("url").getOrElse("None")
    var pstmt: PreparedStatement = null
    var conn: Connection = null
    try {
      conn = DriverManager.getConnection(connectionUrl)
      pstmt = conn.prepareStatement("select testing.compareData(?,?,?)")
      pstmt.setInt(1, id)
      pstmt.setString(2, message)
      pstmt.setString(3, resourceType)
      pstmt.executeQuery()
    }
    catch {
      case e: Exception => throw e
    }
    finally {
      conn.close()
    }
  }  
这里,
testing
是我创建函数的模式。使用以下两种方式运行时,会引发以下错误:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: function testing.comparedata(character varying, integer, character varying) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.

您的第一个参数不是字符串,因此调用
setString(1,…)
将导致您在问题中引用的错误

您的第二个参数被声明为
json
,因此您也不能直接在那里传递字符串值。以下各项应起作用(给定函数定义):


您可能还需要使用
pstmt.execute()
而不是
executeQuery()
,因为您的函数不返回任何内容。

您的第一个参数不是字符串,因此调用
setString(1,…)
将导致您在问题中引用的错误

您的第二个参数被声明为
json
,因此您也不能直接在那里传递字符串值。以下各项应起作用(给定函数定义):


您可能还需要使用
pstmt.execute()
而不是
executeQuery()
,因为您的函数不返回任何内容。

请回答您的问题,并添加用于创建函数化函数的确切
create function
语句。请看一眼我们可以推荐使用一些Scala DB lib吗?请回答您的问题并添加用于创建函数的确切的
create function
语句。请看一眼我们可以推荐使用Scala DB Lib吗?可以,谢谢你的回答。成功了。我只需要做一个小小的改变,就是把
jsonb
改成
json
,因为不知怎么的,它不起作用了,我在你的答案中编辑过。你让我开心:)啊,对了。我没有看到您使用的是
json
而不是
jsonb
(您应该使用)是的,但我不想使用二进制形式,因为稍后其他模块可以对该json执行操作。您不必使用“二进制形式”来处理它,这只是Postgres存储值的另一种方式。“一般来说,大多数应用程序应该更喜欢将JSON数据存储为
jsonb
,除非有非常特殊的需求,例如关于对象键顺序的遗留假设”Ohh,好的。我也要试试。谢谢你指出这一点。而且我对callable语句的方式有点好奇。为什么那部分不起作用?是不是因为那个演员的角色相同?是的,谢谢你的回答。成功了。我只需要做一个小小的改变,就是把
jsonb
改成
json
,因为不知怎么的,它不起作用了,我在你的答案中编辑过。你让我开心:)啊,对了。我没有看到您使用的是
json
而不是
jsonb
(您应该使用)是的,但我不想使用二进制形式,因为稍后其他模块可以对该json执行操作。您不必使用“二进制形式”来处理它,这只是Postgres存储值的另一种方式。“一般来说,大多数应用程序应该更喜欢将JSON数据存储为
jsonb
,除非有非常特殊的需求,例如关于对象键顺序的遗留假设”Ohh,好的。我也要试试。谢谢你指出这一点。而且我对callable语句的方式有点好奇。为什么那部分不起作用?是不是因为那个角色的关系?
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: function testing.comparedata(character varying, integer, character varying) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  pstmt = conn.prepareStatement("select testing.compareData(?,cast(? as json),?)")
  pstmt.setInt(1, id)
  pstmt.setString(2, message)
  pstmt.setString(3, resourceType)