Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Data.SQLite Can't Load Extension

Tags:

c#

sqlite

asp.net

I'm trying to load the new json1 extension when the connection in set up, but I keep receiving this error:

SQL logic error or missing database
The specified procedure could not be found.

Here is my code:

SQLiteConnection connection = (SQLiteConnection)session.Connection;
connection.EnableExtensions(true);
Assembly assembly = Assembly.Load("System.Data.SQLite");
connection.LoadExtension(assembly.Location, "sqlite3_json_init");

I'm using NHibernate, so I just grab the connection from the session before any logic is executed.

Is there something I'm doing wrong? I've also tried loading SQLite.Interop.dll and then calling the sqlite3_json_init method and that still does not work.

I did this and it still doesn't seem to work:

     SQLiteConnection connection = (SQLiteConnection)session.Connection;
                connection.EnableExtensions(true);
                string path = "F:\\GitHub\\ExampleProj\\lib\\sqlite\\SQLite.Interop.dll";
                if (File.Exists(path))
                {
                    connection.LoadExtension(path,
"sqlite3_json_init");
                }
like image 783
The Pax Bisonica Avatar asked Apr 17 '16 00:04

The Pax Bisonica


2 Answers

Loading the extension from SQLite.Interop.dll is the correct way. So you should just be able to do the following:

connection.EnableExtensions(true);
connection.LoadExtension(@"full\path\to\SQLite.Interop.dll", "sqlite3_json_init");

I've tested this and it's working, so if you still have a problem, you may need to indicate how you're getting the path of the interop file. For debugging purposes, maybe add an assertion before LoadExtension to make sure the interop file that you're trying to load actually exists where you think it does.

Also make sure that you're loading the correct version (x86 vs. x64.) If you try the wrong one, you'll get, "The specified module could not be found." on the LoadExtension call even when it's actually there.

like image 123
Mike W. Avatar answered Sep 22 '22 21:09

Mike W.


It actually looks like you do not need to enter in the full path to the interop file.. apparently it identifies which processor architecture you are using and finds the SQLite.Interop.dll file in the bin folder based on that (bin/x64/SQLite.Interop.dll). I just had to give it the name of the interop file and it worked:

 SQLiteConnection connection = (SQLiteConnection)sender;
                connection.EnableExtensions(true);

                connection.LoadExtension("SQLite.Interop.dll", "sqlite3_json_init");
like image 25
The Pax Bisonica Avatar answered Sep 22 '22 21:09

The Pax Bisonica