Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using sqlite extensions with sqlite3 gem #383

Closed
cboulanger opened this issue Mar 16, 2023 · 5 comments
Closed

Using sqlite extensions with sqlite3 gem #383

cboulanger opened this issue Mar 16, 2023 · 5 comments

Comments

@cboulanger
Copy link

cboulanger commented Mar 16, 2023

Hi, I am interested in using the spellfix extension with the sqlite3 gem.

https://www.sqlite.org/spellfix1.html
https://github.com/sqlite/sqlite/blob/master/ext/misc/spellfix.c

is this possible? Of course, it would be great if no compilation step was needed (via the OS sqlite?).

Thank you.

@flavorjones
Copy link
Member

Hi! Thanks for asking this question.

You can load sqlite3 extensions using the SQLite3::Database#load_extension method. It's not terribly well documented, but the general idea is:

db = SQLite3::Database.new(':memory:')
db.enable_load_extension(true)
db.load_extension(path_to_shared_object)

where path_to_shared_object is the path to the .so or .dll or what-have-you.

Does this answer help?

@cboulanger
Copy link
Author

Hi, thanks for your response! I assume that the native sqlite binary that comes with the default gem or bundle install (I am on Ubuntu) doesn't have that (or any other) extension bundled - so I would need to compile it myself as documented here, I guess.

@flavorjones
Copy link
Member

@cboulanger Yep, I was able to do this on my linux machine (after cloning sqlite/sqlite)

# in sqlite directory
gcc -g -fPIC -shared ./ext/misc/spellfix.c -o spellfix.o

then, in ruby:

#! /usr/bin/env ruby

require "sqlite3"

db = SQLite3::Database.new(':memory:')
db.enable_load_extension(true)
db.load_extension("/path/to/sqlite/spellfix.o")
db.execute("CREATE VIRTUAL TABLE demo USING spellfix1;")

I'm going to close this, but please do comment if you have any other questions!

@cboulanger
Copy link
Author

It works! Thanks so much. I've created a small gist for future reference: https://gist.github.com/cboulanger/f9c57fe2258e3b95c2f59ae10096a1f3

@flavorjones
Copy link
Member

Note: I've extracted some of what we talked about here into INSTALLATION.md in #390

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants