xerial / sqlite-jdbc

SQLite JDBC Driver
Apache License 2.0
2.83k stars 619 forks source link

How do I load an extension #908

Open whatify-io opened 1 year ago

whatify-io commented 1 year ago

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:

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):

Additional context Add any other context about the problem here.

whatify-io commented 1 year ago

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 commented 1 year ago

Hello anything someone can come with?

ieugen commented 9 months ago

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

ieugen commented 9 months ago

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.