Sqlite: Execute Spatialite Functions in Intellij

白昼怎懂夜的黑 提交于 2020-01-24 14:18:05

问题


Does anyone know whether it is possible to execute spatialite functions on sqlite-databases in intellij? Reference of spatialite functions: http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.0.html

In particular, I would be interested in using functions on type Point.

Thanks!

Edit: Functions do work within the official spatialite-gui, however I don't think there is a way to use the spatialite-gui programmatically, is there?

Here is what I tried so far: In intellij I connected the Java JDBC library and tried using function ST_X(point) and ST_Y(point) with no success:

        Connection c = null;
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:" + path + databaseName);
        c.setAutoCommit(false);
        System.out.println("Opened database \"" + databaseName + "\" successfully");

        String sql = "SELECT id, ST_Y(point), ST_X(point) from tablename;";

        Statement stmt =  c.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while ( rs.next() ) {
            String msg = "Id: ";
            msg += rs.getInt(1);
            msg += " , Latitude: ";
            msg += rs.getDouble(2);
            msg += " , Longitude: ";
            msg += rs.getDouble(3);
            System.out.println(msg);
        }
        rs.close();
        stmt.close();
        c.close();

This throws the following exception:

Exception in thread "main" java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such function: ST_Y)
    at org.sqlite.core.DB.newSQLException(DB.java:890)
    at org.sqlite.core.DB.newSQLException(DB.java:901)
    at org.sqlite.core.DB.throwex(DB.java:868)
    at org.sqlite.core.NativeDB.prepare(Native Method)
    at org.sqlite.core.DB.prepare(DB.java:211)
    at org.sqlite.jdbc3.JDBC3Statement.executeQuery(JDBC3Statement.java:81)
    at com.company.Test.main(Test.java:77)

Edit 2: I'm lost. It seems that I need to load extensions to make it work. Is this not included in the JDBC connector? (I pulled the JDBC connector through Maven.) Where do I find the correct extensions? Are those the ones? https://www.gaia-gis.it/fossil/libspatialite/index Or those? http://www.gaia-gis.it/gaia-sins/index.html How do I use them? Has anybody done this before?


回答1:


Before performing spatial queries you need to load the spatialite module doing

SELECT load_extension('mod_spatialite');

You can find the documentation here: Dynamically loading SpatiaLite as an extension module.

Note that the module to load must be on the system path (documentation) and the path separator must be /. Also take into account that the spatialite module and Java must be compatibles (both 32 or 64 bits).



来源:https://stackoverflow.com/questions/38612745/sqlite-execute-spatialite-functions-in-intellij

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!