我想在我的颤振应用程序中对本地数据库启用全文搜索。
我正在使用moor_flutter包与数据库进行交互,我看到它提供了一个扩展,可以使用 fts5 进行全文搜索。但是,我无法让它正常工作。
我总是得到以下异常:
no such module: fts5 (Sqlite code 1 SQLITE_ERROR), (OS error - 2:No such file or directory)
重现步骤
- 创建一个新的flutter项目并将以下依赖项添加到
pubspec.yaml
:moor_flutter: ^4.0.0
- 将以下 dev_dependencies 添加到
pubspec.yaml
:build_runner: ^2.1.1
moor_generator: ^4.5.1
- 创建一个
build.yaml
文件并添加以下内容(在docs中指定):
targets:
$default:
builders:
moor_generator:
options:
sqlite:
modules:
- fts5
- moor_ffi
- 在
lib
文件夹中,创建一个tables.moor
包含以下内容的文件:
CREATE TABLE example_table (
name TEXT,
content TEXT
);
CREATE VIRTUAL TABLE example_table_search
USING fts5(
name, content, content='example_table', content_rowid='rowid'
);
CREATE TRIGGER example_table_ai AFTER INSERT ON example_table BEGIN
INSERT INTO example_table_search(name, content)
VALUES (new.name, new.content);
END;
CREATE TRIGGER example_table_ad AFTER DELETE ON example_table BEGIN
INSERT INTO example_table_search(example_table_search, name, content)
VALUES ('delete', old.name, old.content);
END;
CREATE TRIGGER example_table_au AFTER UPDATE ON example_table BEGIN
INSERT INTO example_table_search(example_table_search, name, content)
VALUES ('delete', old.name, old.content);
INSERT INTO example_table_search(name, content)
VALUES (new.name, new.content);
END;
- 在文件夹中创建一个
database.dart
文件lib
:
import 'package:moor/moor.dart';
part 'database.g.dart';
@UseMoor(include: {'./tables.moor'})
class ExampleDatabase extends _$ExampleDatabase {
ExampleDatabase(QueryExecutor e) : super(e);
@override
int get schemaVersion => 1;
}
- 在文件夹中创建一个
main.dart
文件lib
:
import 'package:flutter/material.dart';
import 'package:moor_flutter/moor_flutter.dart';
import './database.dart';
late ExampleDatabase db;
void main() {
WidgetsFlutterBinding.ensureInitialized();
db = ExampleDatabase(
FlutterQueryExecutor(
path: 'db.sqlite',
logStatements: true,
),
);
runApp(MaterialApp(home: HomePage()));
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final tableStream = watchExampleTable();
static Stream<List<ExampleTableData>> watchExampleTable() =>
db.select(db.exampleTable).watch();
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<List<ExampleTableData>>(
stream: tableStream,
builder: (context, snapshot) {
if (snapshot.hasError) return Text('${snapshot.error}');
if (!snapshot.hasData) {
return SizedBox();
}
final data = snapshot.data!;
return ListView.builder(
shrinkWrap: true,
itemCount: data.length,
itemBuilder: (context, index) {
final d = data[index];
return ListTile(
title: Text(d.name ?? 'No Name'),
subtitle: Text(d.content ?? 'No content'),
);
},
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
db.into(db.exampleTable).insert(
ExampleTableCompanion(
content: Value('This is some test content'),
name: Value("Jeff"),
),
);
},
),
);
}
}
- 运行
flutter pub get
和flutter pub run build_runner build
- 运行应用程序,您现在应该会看到异常。
我是否缺少一些额外的配置或依赖项?
可能的解决方案
在写这个问题时,我看到这个答案说 fts5 模块可能不存在或默认启用。
我试图依赖sqlite3和sqlite3_flutter_libs(应该可以使用fts5 扩展名),但不知道如何通过 moor 连接到该数据库。