如何使用TextField小部件作为搜索栏来查询本地SQLite数据库资产的结果?
有没有比使用textfield小部件更好的选择? 例子将不胜感激。
使用代码进行编辑:
SELECT tblApp.AppID, tblRef.Label as [Type], tblRef2.Label as [Status],
tappext.NewField
FROM tblApp tapp LEFT JOIN
tblAppExt tex
ON tapp.AppID = tex.AppID LEFT JOIN
tblRef tref
ON tapp.AppTypeID = tref.ID LEFT JOIN
tblRef tblRef2
ON tapp.AppStatusID = tblRef2.ID OUTER APPLY
( SELECT TOP (1) tappext.*
FROM tblAppExt2 tappext
WHERE tapp.AppID = AppID
ORDER BY ??
) tappext;
这是搜索栏的代码
我的数据库中有一个表的句子,后面有一个段落ID,所以我想搜索像这样的查询
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.all(8.0),
child: new Container(
height: 70.0,
color: Theme.CompanyColors.iBlue,
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Card(
child: new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Icon(Icons.search),
new Container(
width: MediaQuery.of(context).size.width - 100.0,
child: new TextField(
decoration: InputDecoration(
hintText: 'Search',
//onChanged:
//onSearchTextChanged,
),
),
),
答案 0 :(得分:0)
假设您正在使用this plugin。您应该创建并填充数据库(plugin's docs)。
然后,在搜索小部件中,使用以下示例:
class _MyHomePageState extends State<MyHomePage> {
Database database;
@override
void initState() {
// open the database
openDatabase('pathToDb', version: 1,
onCreate: (Database db, int version) async {
database = db;
// When creating the db, create the table
});
super.initState();
}
@override
Widget build(BuildContext context) {
if (database == null) {
return CircularProgressIndicator();
}
return Container(
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Container(
padding: const EdgeInsets.all(8.0),
child: new Container(
height: 70.0,
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Card(
child: new Container(
child: new Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
new Icon(Icons.search),
new Container(
width: MediaQuery.of(context).size.width - 100.0,
child: new TextField(
decoration: InputDecoration(
hintText: 'Search',
//onSearchTextChanged,
),
onChanged: (String text) async {
List<Map> res = await database.rawQuery(
"SELECT * FROM sentences WHERE title LIKE '%${text}%' OR body LIKE '%${text}%'");
print(res);
}),
)
]))))))
]));
}
}
注意:如果多个窗口小部件可以访问数据库,请使用SQL帮助器。阅读文档“ SQL帮助器”部分。这个例子只是您的例子。