𝗛𝗼𝘄 𝘁𝗼 𝗨𝘀𝗲 𝗖𝘂𝘀𝘁𝗼𝗺 𝗦𝗤𝗟𝗶𝘁𝗲 𝗘𝘅𝘁𝗲𝗻𝘀𝗶𝗼𝗻𝘀 𝗶𝗻 𝗖𝗮𝗽𝗮𝗰𝗶𝘁𝗼𝗿
Standard SQLite works for most tasks. Sometimes you need more.
You might need a custom tokenizer for specific languages. You might need custom math functions or special text processing. These are called loadable extensions.
The Capacitor SQLite plugin supports these on Android and iOS.
Why use extensions?
- Custom FTS5 tokenizers: Control how text splits for search.
- Custom SQL functions: Run complex logic close to your data.
The implementation differs by platform.
Android
Android system SQLite does not support loading extensions directly. You must use the requery backend.
- Enable the requery option in your variables.gradle file.
- Add the JitPack repository to your build.gradle file.
- Compile your C code into native libraries (.so files) for each CPU architecture.
- Place these files in the jniLibs folder.
- Use the androidExtensions option when you open the database.
iOS
iOS apps cannot load dynamic libraries at runtime. You must link your extension statically.
- Add your C source file to your Xcode project.
- Set the -DSQLITE_CORE compiler flag for that file.
- Declare the init function in your bridging header.
- Register the extension in your AppDelegate using sqlite3_auto_extension.
The C code for your extension stays the same. Only the build and registration steps change.
Once loaded, your custom extension works just like a built-in SQLite feature. You can use your new tokenizer or function in your SQL statements immediately.
Custom extensions give you the power of native code within your mobile database.
Source: https://dev.to/capawesome/how-to-use-custom-sqlite-extensions-in-capacitor-l5k