This note shows how to load simple as a SQLite runtime extension in a React
Native app using @op-engineering/op-sqlite.
simple is an FTS5 tokenizer extension, so op-sqlite must be built with FTS5
enabled and with runtime extension loading available.
Build an iOS xcframework:
./build-ios-dynamic.shThe output is:
output/libsimple-dynamic.xcframework
For Android, use the shared library from the normal Android build/release
artifact and package it as libsimple.so for each target ABI.
Add the xcframework to your app, for example:
ios/Frameworks/simple.xcframework
If you use CocoaPods, create a small local podspec:
Pod::Spec.new do |s|
s.name = 'SimpleSQLiteExtension'
s.version = '0.1.0'
s.summary = 'SQLite simple tokenizer extension'
s.homepage = 'https://github.com/wangfenjin/simple'
s.license = { :type => 'MIT' }
s.author = { 'simple contributors' => 'https://github.com/wangfenjin/simple' }
s.source = { :git => 'https://github.com/wangfenjin/simple.git', :tag => s.version.to_s }
s.platform = :ios, '12.0'
s.vendored_frameworks = 'Frameworks/simple.xcframework'
endThen reference it from ios/Podfile:
target 'YourApp' do
pod 'SimpleSQLiteExtension', :path => '.'
endRun:
cd ios
pod installPackage libsimple.so in your React Native project, for example:
android/app/src/main/jniLibs/arm64-v8a/libsimple.so
android/app/src/main/jniLibs/armeabi-v7a/libsimple.so
android/app/src/main/jniLibs/x86/libsimple.so
android/app/src/main/jniLibs/x86_64/libsimple.so
Load the extension before creating FTS5 tables that use the simple tokenizer.
import { Platform } from 'react-native';
import { getDylibPath, open } from '@op-engineering/op-sqlite';
const db = open({ name: 'search.db' });
if (Platform.OS === 'android') {
db.loadExtension('libsimple', 'sqlite3_simple_init');
} else if (Platform.OS === 'ios') {
const path = getDylibPath('com.wangfenjin.simple', 'simple');
db.loadExtension(path, 'sqlite3_simple_init');
}After loading succeeds, simple can be used like any other FTS5 tokenizer:
db.execute(`
CREATE VIRTUAL TABLE IF NOT EXISTS docs
USING fts5(title, body, tokenize = 'simple');
`);
db.execute('INSERT INTO docs(title, body) VALUES (?, ?)', [
'中华人民共和国国歌',
'支持中文和拼音搜索',
]);
const result = db.execute(
'SELECT rowid, title FROM docs WHERE docs MATCH simple_query(?)',
['中华国歌'],
);To disable pinyin tokens, use the tokenizer option supported by simple:
db.execute(`
CREATE VIRTUAL TABLE IF NOT EXISTS docs_no_pinyin
USING fts5(title, body, tokenize = 'simple 0');
`);no such tokenizer: simpleusually means the extension was not loaded before the FTS5 table was created.not authorizedorload extensionerrors usually mean runtime extension loading is disabled in the SQLite build used by your app.- iOS apps should load the framework from the app bundle. With op-sqlite, use
getDylibPath('com.wangfenjin.simple', 'simple')for the framework namedsimple.framework. - Android should load the library name without the
.sosuffix:db.loadExtension('libsimple', 'sqlite3_simple_init').