本文介绍了使用Java或Groovy为SQLite编写用户定义的SQL函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用SQLite,用户定义的SQL函数可以很容易地使用 C api 或 PHP 。但是,使用Java还是Groovy也是可以的?解析方案 原来写一个用户定义的函数实际上很容易使用 SQLiteJDBC 。这里有一个Groovy的例子: pre $ code $ @GrabConfig(systemClassLoader = true) @Grab('org.xerial:sqlite- jdbc:3.6.16') import org.sqlite。* import java.sql。* db = groovy.sql.Sql.newInstance(jdbc:sqlite: :memory:,org.sqlite.JDBC) //使用球形余弦定律的距离函数 Function.create(db.getConnection(),distance, new function(){ protected void xFunc()throws SQLException { def lat1 = value_double(0) def lon1 = value_double(1) def lat2 = value_double(2) def lon2 = value_double(3) double theta = lon1 - lon2; double dist =(Math.sin(deg2rad(lat1))* Math.sin(deg2rad(数学公式(deg2rad(lat1))* Math.cos(deg2rad(lat2))* Math.cos(deg2radθ)) dist = Math.acos dist) dist = rad2deg(dist) dist = dist * 60 * 1.1515 dist = dist * 1.609344 结果(DIST); } def deg2rad(deg){ deg * Math.PI / 180.0 } def rad2deg(rad){ rad * 180.0 / Math.PI } }) db.execute(CREATE TABLE city(name,lat,lon)) db.execute(INSERT INTO city(name,lat,lon)VALUES('New York City',40.7143,-74.0060)) db.execute(INSERT INTO city(name,lat,lon)VALUES ('San Francisco',37.7749,-122.4194)) db.execute(INSERT INTO city(name,lat,lon)VALUES('Paris',48.8567,2.3510)) db。执行(INSERT INTO city(name,lat,lon)VALUES('Cologne',50.9407,6.9599)) db.eachRow(SELECT a.name as a,b.name as b ,距离(a.lat,a.lon,b.lat,b.lon)作为d从城市a,城市b WHERE a.name!= b.name ORDER BY d;){ println距离从$ {it.a}到$ {it.b}:$ {it.d} km 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? 解决方案 It turns out writing a user defined function is actually quite easy using SQLiteJDBC. Here's a Groovy example:@GrabConfig(systemClassLoader=true)@Grab('org.xerial:sqlite-jdbc:3.6.16')import org.sqlite.*import java.sql.*db = groovy.sql.Sql.newInstance("jdbc:sqlite::memory:","org.sqlite.JDBC")// a distance function using the spherical law of cosinesFunction.create(db.getConnection(), "distance", new Function() { protected void xFunc() throws SQLException { def lat1 = value_double(0) def lon1 = value_double(1) def lat2 = value_double(2) def lon2 = value_double(3) double theta = lon1 - lon2; double dist = (Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))) + (Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta))) dist = Math.acos(dist) dist = rad2deg(dist) dist = dist * 60 * 1.1515 dist = dist * 1.609344 result(dist); } def deg2rad(deg) { deg * Math.PI / 180.0 } def rad2deg(rad) { rad * 180.0 / Math.PI }})db.execute("CREATE TABLE city(name, lat, lon)")db.execute("INSERT INTO city(name, lat, lon) VALUES('New York City', 40.7143, -74.0060)")db.execute("INSERT INTO city(name, lat, lon) VALUES('San Francisco', 37.7749, -122.4194)")db.execute("INSERT INTO city(name, lat, lon) VALUES('Paris', 48.8567, 2.3510)")db.execute("INSERT INTO city(name, lat, lon) VALUES('Cologne', 50.9407, 6.9599)")db.eachRow("SELECT a.name as a, b.name as b, distance(a.lat, a.lon, b.lat, b.lon) as d FROM city a, city b WHERE a.name != b.name ORDER BY d;") { println "Distance from ${it.a} to ${it.b}: ${it.d}km"} 这篇关于使用Java或Groovy为SQLite编写用户定义的SQL函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 13:25