Writing user defined SQL functions for SQLite using Java or Groovy?

前端 未结 4 1775
轮回少年
轮回少年 2020-12-10 19:44

With SQLite, user defined SQL functions can easily be added using the C api or PHP. But is it also possible using Java or Groovy?

4条回答
  •  醉话见心
    2020-12-10 20:23

    Simple way to create a function which accepts arguments and return result:

            Function.create(conn, "addNS", new Function() {
                @Override
                protected void xFunc() {
                    System.out.println("myFunc called!");
                    String arg1;
                    try {
                        arg1 = value_text(0);
                        System.out.println("function arg1:"+arg1);
                        result("NS-"+arg1);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            },  1, Function.FLAG_DETERMINISTIC);
    
            rs = conn.createStatement().executeQuery("select addNS('xyz');");
            while(rs.next()) {
                String val = rs.getString(1);
                System.out.println("Function return val : " + val);
            }
    

提交回复
热议问题