How to load SpatiaLite extension in JAVA

2,399 views
Skip to first unread message

Thomas

unread,
Mar 9, 2010, 9:14:22 AM3/9/10
to SpatiaLite Users
Dear all,

I want to load libspatialite-1.dll into my JAVA program, but I don't
get it to work properly
So far I've got this:
stmt.executeQuery("SELECT load_extension('libspatialite.dll')");

The error message I get is:

java.sql.SQLException: no such function: load_extension

I've got all the necessary libraries in the C:/windows/system32 folder
and it does work in sqlite3.

Thanks in advance,

Thomas

Thomas

unread,
Mar 9, 2010, 9:18:36 AM3/9/10
to SpatiaLite Users
Just a small addition, there is a small typo in the statement.
libspatialite should be libspatialite-1

Justin Deoliveira

unread,
Mar 9, 2010, 9:22:33 AM3/9/10
to spatiali...@googlegroups.com
Hi Thomas,

You need to ensure that sqlite has been compiled to allow for the
loading of shared libraries (--enable-dynamic-extensions).

I have had good luck using sqlite/spatialite through java with the
xerial jdbc driver:

http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC

-Justin


--
Justin Deoliveira
OpenGeo - http://opengeo.org
Enterprise support for open source geospatial.

Thomas

unread,
Mar 9, 2010, 10:10:16 AM3/9/10
to SpatiaLite Users
Hi Justin,

I've tried using the drivers from the site you provided (I used a
different driver before). Now I'm getting a different error.

This is my code:

public Database(String host, String database){
makeConnection();
try {
stmt = con.createStatement();
stmt.executeQuery("SELECT load_extension('libspatialite-1.dll')");
} catch (SQLException e) {
e.printStackTrace();
}
}
//create connection
public void makeConnection(){
try{
Class.forName("org.sqlite.JDBC").newInstance();
String url = "jdbc:sqlite:mars3.sqlite";
con = DriverManager.getConnection(url);

}
catch(Exception ex){
ex.printStackTrace();
}
System.out.println("Connection succeeded");
}

And this is the error I'm getting.

[SQLITE_ERROR] SQL error or missing database (not authorized)

I hope you can help.

Thomas

> OpenGeo -http://opengeo.org

Francisco Bosch

unread,
May 4, 2010, 7:48:36 AM5/4/10
to SpatiaLite Users
Hi Thomas,

I'm trying to use spatialite in a java program with the SQLiteJDBC
from the xerial website,
Did you achive use spatialite with java? because I have the same error
than you.

So far I can use sqlite in java but no spatialite.

thanks.
--
Francisco Bosch
--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To post to this group, send email to spatiali...@googlegroups.com.
To unsubscribe from this group, send email to spatialite-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spatialite-users?hl=en.

Frans Knibbe

unread,
Sep 6, 2011, 7:39:20 AM9/6/11
to spatiali...@googlegroups.com
I have the same problem. I have a database that I want to access with C# and java. In C#, I can access the database and use the spatial things provided by SpatiaLite. This is done with the load_extension command. In java, I can access the same database but I have not been able to succesfully load the SpatiaLite extension. Different JDBC drivers give different errors, as described in the earlier messages.

Can someone explain how to connect to SpatiaLite from java?

Andrea P.

unread,
Sep 6, 2011, 8:11:41 AM9/6/11
to SpatiaLite Users


>And this is the error I'm getting.
>
>[SQLITE_ERROR] SQL error or missing database (not authorized)
>
>I hope you can help.
>
>Thomas

Hi,
I have successfully to load_extension using the xerial jdbc driver.

The more troubles I found was to ensure the
libspatialite-1.dll
could find all the dependendies (other dll) it need.
Also pay attention to put the database (the db sqlite) in the right
position.

I guess using
Class.forName("org.sqlite.JDBC").newInstance();
String url = "jdbc:sqlite:mars3.sqlite";

you are refering to where is the JVM.

So perhaps is better use a absolute path for database.

Regards,
Andrea.

Andrea P.

unread,
Sep 6, 2011, 8:13:35 AM9/6/11
to SpatiaLite Users
Hi,
I use this xerial driver:
http://www.xerial.org/maven/repository/snapshot/org/xerial/sqlite-jdbc/3.7.7.1-SNAPSHOT/

is a snapsot, but it is aligned to the sqlite used in the last
spatialite (3.0.0 beta).

Regards.

Frans Knibbe

unread,
Sep 7, 2011, 10:50:42 AM9/7/11
to spatiali...@googlegroups.com
Thanks. By now I have figured out that the problem has to do with me running the 64 version of Windows 7. I was able to get SpatiaLite to work with java on the 32 bit version. I will continue on the thread(s) about Windows 7 64 bit problems. 

Masud Rashid

unread,
Oct 15, 2012, 4:36:58 AM10/15/12
to spatiali...@googlegroups.com
Frans knibbe,
Did you solve the problem with Windows 64 bit. I am having trouble with this.

kavimani durai

unread,
Feb 14, 2018, 4:01:09 AM2/14/18
to SpatiaLite Users
Please check Java 32 or 64 BIT 
Reply all
Reply to author
Forward
0 new messages