1

A SQLite database contains a column with JSON data like the following:

MyTable
______________________________________
| specs         | name   | date
--------------------------------------
| ["foo","bar"] | Test01 | 2018-02-05
| ["foo","bar"] | Test02 | 2018-02-01
| ["foo"]       | Test03 | 2018-02-03

Pseudo-code:

private static SQLiteConnection _sqliteDb;

using (var source = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
{
    source.Open();

    _sqliteDb = new SQLiteConnection("Data Source=:memory:");
    _sqliteDb.Open();

    // copy db file to memory
    source.BackupDatabase(_sqliteDb, "main", "main", -1, null, 0);
    source.Close();
}

var cmd = new SQLiteCommand(query, _sqliteDb);

In order to filter on this column, the query looks like this:

var query = "SELECT * FROM MyTable, json_each(MyTable.specs) AS spec WHERE spec.value = 'bar'"

This error appears: SQLite error (1): no such table: json_each

How to use json1 extension with System.Data.SQLite.Core?

4
  • Did you follow the instructions here? Commented Feb 4, 2018 at 18:35
  • Yes, my query is similar to the one in 4.13.1. Examples using json_each() Commented Feb 5, 2018 at 8:29
  • Is specs the table name or the column name? (The other one is missing from the query.) Commented Feb 5, 2018 at 18:37
  • It is a column name. I edited my post. Commented Feb 5, 2018 at 20:23

1 Answer 1

4

SQLite extensions weren't loaded.

Right after:

_sqliteDb.Open();

I must call the following in order to make json1 to work:

_sqliteDb.EnableExtensions(true);
_sqliteDb.LoadExtension("SQLite.Interop.dll", "sqlite3_json_init");

Also note that functions are registered per-connection.

Sign up to request clarification or add additional context in comments.

1 Comment

The LoadExtension is failing to find that dll. What could be the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.