网站注册表单怎么做,网站设计照着做 算侵权吗,惠安县住房和城乡规划建设局网站,用wordpress制作表单flutter开发实战-本地SQLite数据库存储
正在编写一个需要持久化且查询大量本地设备数据的 app#xff0c;可考虑采用数据库。相比于其他本地持久化方案来说#xff0c;数据库能够提供更为迅速的插入、更新、查询功能。这里需要用到sqflite package 来使用 SQLite 数据库
预…flutter开发实战-本地SQLite数据库存储
正在编写一个需要持久化且查询大量本地设备数据的 app可考虑采用数据库。相比于其他本地持久化方案来说数据库能够提供更为迅速的插入、更新、查询功能。这里需要用到sqflite package 来使用 SQLite 数据库
预览图
一、引入sqflite
在工程的pubspec.yaml中引入插件 # sqflitesqflite: ^2.2.84
二、使用sqflite
使用 sqflite 实现插入读取更新删除数据。
打开数据库 Futurevoid openDB(BuildContext context) async {// Open the database and store the reference.database await openDatabase(// Set the path to the database. Note: Using the join function from the// path package is best practice to ensure the path is correctly// constructed for each platform.join(await getDatabasesPath(), doggie_database.db),// When the database is first created, create a table to store dogs.onCreate: (db, version) {// Run the CREATE TABLE statement on the database.return db.execute(CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER),);},version: 1,);}
插入一条记录
Futurevoid insertDB(BuildContext context) async {dogId;// Create a Dog and add it to the dogs tablevar fido Dog(id: dogId,name: Fido,age: 35,);// Get a reference to the database.final db await database;// Insert the Dog into the correct table. You might also specify the// conflictAlgorithm to use in case the same dog is inserted twice.//// In this case, replace any previous data.await db?.insert(dogs,fido.toMap(),conflictAlgorithm: ConflictAlgorithm.replace,);}
更新一条记录 Futurevoid updateDog(Dog dog) async {// Get a reference to the database.final db await database;// Update the given Dog.await db?.update(dogs,dog.toMap(),// Ensure that the Dog has a matching id.where: id ?,// Pass the Dogs id as a whereArg to prevent SQL injection.whereArgs: [dog.id],);}
删除一条记录 Futurevoid deleteDog(int id) async {// Get a reference to the database.final db await database;// Remove the Dog from the database.await db?.delete(dogs,// Use a where clause to delete a specific dog.where: id ?,// Pass the Dogs id as a whereArg to prevent SQL injection.whereArgs: [id],);}
获取存储记录 // A method that retrieves all the dogs from the dogs table.FutureListDog dogs() async {// Get a reference to the database.final db await database;// Query the table for all the dogs.final ListMapString, Object?? dogMaps await db?.query(dogs);if (dogMaps ! null dogMaps.isNotEmpty) {// Convert the list of each dogs fields into a list of Dog objects.ListDog dogs [];for(var dogMap in dogMaps) {// Dog dog Dog(id: dogMap[id]??0, name: name, age: age)var id dogMap[id] as int;var name dogMap[name] as String;var age dogMap[age] as int;Dog dog Dog(id: id, name: name, age: age);dogs.add(dog);}return dogs;}return [];}
完整代码如下
import package:flutter/material.dart;
import dart:async;import package:flutter/widgets.dart;
import package:path/path.dart;
import package:sqflite/sqflite.dart;class SqliteDemoPage extends StatefulWidget {const SqliteDemoPage({super.key});overrideStateSqliteDemoPage createState() _SqliteDemoPageState();
}class _SqliteDemoPageState extends StateSqliteDemoPage {Database? database;int dogId 0;Futurevoid openDB(BuildContext context) async {// Open the database and store the reference.database await openDatabase(// Set the path to the database. Note: Using the join function from the// path package is best practice to ensure the path is correctly// constructed for each platform.join(await getDatabasesPath(), doggie_database.db),// When the database is first created, create a table to store dogs.onCreate: (db, version) {// Run the CREATE TABLE statement on the database.return db.execute(CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER),);},version: 1,);}// A method that retrieves all the dogs from the dogs table.FutureListDog dogs() async {// Get a reference to the database.final db await database;// Query the table for all the dogs.final ListMapString, Object?? dogMaps await db?.query(dogs);if (dogMaps ! null dogMaps.isNotEmpty) {// Convert the list of each dogs fields into a list of Dog objects.ListDog dogs [];for(var dogMap in dogMaps) {// Dog dog Dog(id: dogMap[id]??0, name: name, age: age)var id dogMap[id] as int;var name dogMap[name] as String;var age dogMap[age] as int;Dog dog Dog(id: id, name: name, age: age);dogs.add(dog);}return dogs;}return [];}Futurevoid updateDog(Dog dog) async {// Get a reference to the database.final db await database;// Update the given Dog.await db?.update(dogs,dog.toMap(),// Ensure that the Dog has a matching id.where: id ?,// Pass the Dogs id as a whereArg to prevent SQL injection.whereArgs: [dog.id],);}Futurevoid deleteDog(int id) async {// Get a reference to the database.final db await database;// Remove the Dog from the database.await db?.delete(dogs,// Use a where clause to delete a specific dog.where: id ?,// Pass the Dogs id as a whereArg to prevent SQL injection.whereArgs: [id],);}Futurevoid insertDB(BuildContext context) async {dogId;// Create a Dog and add it to the dogs tablevar fido Dog(id: dogId,name: Fido,age: 35,);// Get a reference to the database.final db await database;// Insert the Dog into the correct table. You might also specify the// conflictAlgorithm to use in case the same dog is inserted twice.//// In this case, replace any previous data.await db?.insert(dogs,fido.toMap(),conflictAlgorithm: ConflictAlgorithm.replace,);}Futurevoid getListFromDB(BuildContext context) async {ListDog list await dogs();for(var dog in list) {print(dog info:${dog.toString()});}}void updateDB(BuildContext context) {var dog Dog(id: dogId,name: AFarah,age: 11,);updateDog(dog);}Futurevoid deleteDB(BuildContext context) async {await deleteDog(dogId);dogId--;}overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text(SqliteDemo),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [const SizedBox(height: 20,),TextButton(onPressed: () {openDB(context);},child: Container(height: 36,width: 200,color: Colors.lightGreen,alignment: Alignment.center,child: const Text(openDatabase,style: TextStyle(fontSize: 12, color: Colors.white),),),),const SizedBox(height: 20,),TextButton(onPressed: () {insertDB(context);},child: Container(height: 36,width: 200,color: Colors.lightGreen,alignment: Alignment.center,child: const Text(插入一条dog记录,style: TextStyle(fontSize: 12, color: Colors.white),),),),const SizedBox(height: 20,),TextButton(onPressed: () {getListFromDB(context);},child: Container(height: 36,width: 200,color: Colors.lightGreen,alignment: Alignment.center,child: const Text(获取记录列表,style: TextStyle(fontSize: 12, color: Colors.white),),),),const SizedBox(height: 20,),TextButton(onPressed: () {updateDB(context);},child: Container(height: 36,width: 200,color: Colors.lightGreen,alignment: Alignment.center,child: const Text(更新一条记录,style: TextStyle(fontSize: 12, color: Colors.white),),),),const SizedBox(height: 20,),TextButton(onPressed: () {deleteDB(context);},child: Container(height: 36,width: 200,color: Colors.lightGreen,alignment: Alignment.center,child: const Text(删除一条记录,style: TextStyle(fontSize: 12, color: Colors.white),),),),],),),);}
}class Dog {final int id;final String name;final int age;const Dog({required this.id,required this.name,required this.age,});// Convert a Dog into a Map. The keys must correspond to the names of the// columns in the database.MapString, Object? toMap() {return {id: id,name: name,age: age,};}// Implement toString to make it easier to see information about// each dog when using the print statement.overrideString toString() {return Dog{id: $id, name: $name, age: $age};}
}
三、小结
flutter开发实战-本地SQLite数据库存储
学习记录每天不停进步。 文章转载自: http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.sbrxm.cn.gov.cn.sbrxm.cn http://www.morning.wyrkp.cn.gov.cn.wyrkp.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.xqxlb.cn.gov.cn.xqxlb.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.nfgbf.cn.gov.cn.nfgbf.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.rykgh.cn.gov.cn.rykgh.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.lsnbx.cn.gov.cn.lsnbx.cn http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.qhrdx.cn.gov.cn.qhrdx.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.qbfkz.cn.gov.cn.qbfkz.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.fglyb.cn.gov.cn.fglyb.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.xhjjs.cn.gov.cn.xhjjs.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.dpruuode.cn.gov.cn.dpruuode.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.kgjyy.cn.gov.cn.kgjyy.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.bpmnx.cn.gov.cn.bpmnx.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn