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

android 如何自行加载扩展? #1

Closed
Edison0716 opened this issue Nov 1, 2024 · 8 comments
Closed

android 如何自行加载扩展? #1

Edison0716 opened this issue Nov 1, 2024 · 8 comments

Comments

@Edison0716
Copy link

No description provided.

@Edison0716
Copy link
Author

看demo 是针对 Flutter 的,如果我集成 sqlite-android 后,如何在使用时将 Simple 分词器 与 sqlite-android 支持FTS 5 关联在一起呢?在C++ native侧建立连接吗?

@SageMik
Copy link
Owner

SageMik commented Nov 17, 2024

原理和 https://github.com/SageMik/sqlite3_simple/blob/master/lib/src/sqlite3_simple.dart 加载扩展差不多,只要 Android 这边找到等价于 ensureExtensionLoaded() 的 自定义扩展加载函数,就能加载 Simple 扩展。

sqlite-android 为例,在引入 sqlite-android 和 simple-native-android 依赖后,通过前者提供的方法加载自定义扩展:

private fun createSQLiteOpenHelper(): SupportSQLiteOpenHelper {
        val extensionConfigs = mutableListOf(
            RequerySQLiteOpenHelperFactory.ConfigurationOptions {
                it.apply {
                    customExtensions.add(
                        SQLiteCustomExtension("libsimple.so", "sqlite3_simple_init") // 加载 Simple 扩展
                    )
                }
            }
        )
        return RequerySQLiteOpenHelperFactory(extensionConfigs).create(
            SupportSQLiteOpenHelper.Configuration(
                this, ":memory:", object : SupportSQLiteOpenHelper.Callback(1) {
                    override fun onCreate(db: SupportSQLiteDatabase) {
                        db.execSQL(
                            "CREATE VIRTUAL TABLE t1 USING fts5(text, tokenize = 'simple');" +
                                    "INSERT INTO t1 VALUES ('中华人民共和国国歌');"
                        )
                    }

                    override fun onUpgrade(
                        db: SupportSQLiteDatabase,
                        oldVersion: Int,
                        newVersion: Int
                    ) {}
                }
            )
        )
    }

然后在需要的地方进行查询即可:

val dbHelper = createSQLiteOpenHelper()
val db = dbHelper.writableDatabase
val result = db.query("SELECT simple_highlight(t1, 0, '[', ']') FROM t1 WHERE text MATCH simple_query('中华国歌');")
result.moveToFirst()
println(result.getString(0)) // 打印结果:[中华]人民共和[国国歌]

如果需要结巴分词,同样要像 Flutter 这边将 字典文件 保存到可以访问的位置,然后参照 Simple 的说明 通过 SELECT jieba_dict('字典文件路径') 修改 Simple 扩展加载字典文件的路径。

@Edison0716
Copy link
Author

Ok, 感谢 试了一下是可以的。

@Edison0716
Copy link
Author

@SageMik

@Edison0716
Copy link
Author

image
image
jieba query 有 native crash

@Edison0716 Edison0716 reopened this Dec 20, 2024
@SageMik
Copy link
Owner

SageMik commented Dec 20, 2024

如果需要结巴分词,同样要像 Flutter 这边将 字典文件 保存到可以访问的位置,然后参照 Simple 的说明 通过 SELECT jieba_dict('字典文件路径') 修改 Simple 扩展加载字典文件的路径。

@Edison0716 看报错似乎是没有加载字典文件,这个需要按我前面所说的,可以参考 sqlite3_simple 的实现,加载结巴分词的字典文件,这个库只包含 Simple 的 Native 库,不带字典文件也没有相关加载逻辑

@Edison0716
Copy link
Author

ok, 我理解 WCDB 的库 也是可以用我们的simple分词器吧

@SageMik
Copy link
Owner

SageMik commented Dec 20, 2024

ok, 我理解 WCDB 的库 也是可以用我们的simple分词器吧

@Edison0716 我粗略地看了一下 WCDB ,不确定是否能用,你可以询问一下原作者:Simple

@SageMik SageMik closed this as completed Jan 24, 2025
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