sqlite-jdbc icon indicating copy to clipboard operation
sqlite-jdbc copied to clipboard

How do I load an extension

Open whatify-io opened this issue 2 years ago • 7 comments

Describe the bug I added memstat extension by copying code from https://sqlite.org/src/file/ext/misc/memstat.c and pasting it in src/main/ext. Then i did:

  • update makefile with -DSQLITE_DEFAULT_MEMSTATUS=1 -DSQLITE_ENABLE_MEMSTATVTAB=1
  • make native (mine is linux x86_64)
  • mvn package

This produced the jar.

Now I copied the Sample.java, showing relevant section below:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
 import org.sqlite.core.DB;
import org.sqlite.SQLiteConnection;
    import java.util.Properties;

    public class Sample
    {
      public static void main(String[] args)
      {
        Connection connection = null;
        Properties properties = new Properties();
        properties.setProperty("enable_load_extension", "true");

        try
        {
          // create a database connection
          connection = DriverManager.getConnection("jdbc:sqlite:memory:", properties);
            // sql queries to load extensions
        //  connection.prepareStatement("SELECT load_extension('memstat')").execute(); -> perhaps this is not needed?
          Statement sm = connection.createStatement();
          ResultSet rr = sm.executeQuery("select * from memstat"); -> throws no table , tried sqlite_memstat too
          while(rr.next()) {
                System.out.println(rr.getString("name"));
          }
        

I was able to use a .so file and load that but I want to compile it together as done for extension-functions

Expected behavior I expected to see memstat table.

Logs If applicable, provide logs.

Environment (please complete the following information):

  • OS: Linux
  • CPU architecture: x86_64
  • sqlite-jdbc version 3.39.2.0

Additional context Add any other context about the problem here.

whatify-io avatar May 23 '23 08:05 whatify-io

Hi can I get some help here would be really great, as this is blocking us from getting sqlite heap stats. It would be good to add some example on how to add an extension to src/main/ext and use it. I don't know if there is a JDBC way of doing this http://www.sqlite.org/c3ref/db_status.html

whatify-io avatar May 24 '23 17:05 whatify-io

Hello anything someone can come with?

whatify-io avatar Jun 23 '23 21:06 whatify-io

Hi @whatify-io , did you manage to find a solution to this?

ieugen avatar Jan 22 '24 10:01 ieugen

it seems there is load_extension in JNI to allow loading of extensions.

https://github.com/xerial/sqlite-jdbc/blob/c024a88d991a467e47781b611c54bf1d1877b290/src/main/java/org/sqlite/core/NativeDB.java#L108

Have your tried it @whatify-io ? After calling it I imagine sqlite should allow for extension loading - even via SQL function.

ieugen avatar Jan 22 '24 11:01 ieugen

This is silly. Does anyone have an alternative driver that's maintained by more than one person? Or maybe a contributing guide so I can help make this usable?

Not being able to load extensions is kind of a huge issue.

sylv256 avatar Oct 02 '25 01:10 sylv256

This is silly. Does anyone have an alternative driver that's maintained by more than one person? Or maybe a contributing guide so I can help make this usable?

Not being able to load extensions is kind of a huge issue.

You just need to use load_extension in a statement.

UPDATE: To clarify, OP is asking how to include a source file extension in the build. We do not support this as of now. What we do support is runtime loadable extension.

gotson avatar Oct 02 '25 02:10 gotson

Documentation clarified: https://github.com/xerial/sqlite-jdbc/blob/master/USAGE.md#how-to-load-run-time-loadable-extensions

gotson avatar Oct 03 '25 09:10 gotson