当前位置: 首页 > news >正文

flutter开发实战-本地SQLite数据存储

flutter开发实战-本地SQLite数据库存储

正在编写一个需要持久化且查询大量本地设备数据的 app,可考虑采用数据库。相比于其他本地持久化方案来说,数据库能够提供更为迅速的插入、更新、查询功能。这里需要用到sqflite package 来使用 SQLite 数据库

预览图
在这里插入图片描述

一、引入sqflite

在工程的pubspec.yaml中引入插件

  # sqflitesqflite: ^2.2.8+4

二、使用sqflite

使用 sqflite 实现插入,读取,更新,删除数据。

  • 打开数据库
  Future<void> 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,);}
  • 插入一条记录
Future<void> 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,);}
  • 更新一条记录
  Future<void> 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 Dog's id as a whereArg to prevent SQL injection.whereArgs: [dog.id],);}
  • 删除一条记录
  Future<void> 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 Dog's id as a whereArg to prevent SQL injection.whereArgs: [id],);}
  • 获取存储记录
  // A method that retrieves all the dogs from the dogs table.Future<List<Dog>> dogs() async {// Get a reference to the database.final db = await database;// Query the table for all the dogs.final List<Map<String, Object?>>? dogMaps = await db?.query('dogs');if (dogMaps != null && dogMaps.isNotEmpty) {// Convert the list of each dog's fields into a list of `Dog` objects.List<Dog> 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});@overrideState<SqliteDemoPage> createState() => _SqliteDemoPageState();
}class _SqliteDemoPageState extends State<SqliteDemoPage> {Database? database;int dogId = 0;Future<void> 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.Future<List<Dog>> dogs() async {// Get a reference to the database.final db = await database;// Query the table for all the dogs.final List<Map<String, Object?>>? dogMaps = await db?.query('dogs');if (dogMaps != null && dogMaps.isNotEmpty) {// Convert the list of each dog's fields into a list of `Dog` objects.List<Dog> 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 [];}Future<void> 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 Dog's id as a whereArg to prevent SQL injection.whereArgs: [dog.id],);}Future<void> 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 Dog's id as a whereArg to prevent SQL injection.whereArgs: [id],);}Future<void> 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,);}Future<void> getListFromDB(BuildContext context) async {List<Dog> 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);}Future<void> 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.Map<String, 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数据库存储

学习记录,每天不停进步。

相关文章:

flutter开发实战-本地SQLite数据存储

flutter开发实战-本地SQLite数据库存储 正在编写一个需要持久化且查询大量本地设备数据的 app&#xff0c;可考虑采用数据库。相比于其他本地持久化方案来说&#xff0c;数据库能够提供更为迅速的插入、更新、查询功能。这里需要用到sqflite package 来使用 SQLite 数据库 预…...

【路由組件】

完成Vue Router 安装后&#xff0c;就可以使用路由了&#xff0c;路由的基本使用步骤&#xff0c;首先定义路由组件&#xff0c;以便使用Vue Router控制路由组件展示与 切换&#xff0c;接着定义路由链接和路由视图&#xff0c;以便告知路由组件渲染到哪个位置&#xff0c;然后…...

【C++风云录】数字逻辑设计优化:电子设计自动化与集成电路

集成电路设计&#xff1a;打开知识的大门 前言 本文将详细介绍关于数字芯片设计&#xff0c;电子设计格式解析&#xff0c;集成电路设计工具&#xff0c;硬件描述语言分析&#xff0c;电路验证以及电路优化六个主题的深入研究与实践。每一部分都包含了主题的概述&#xff0c;…...

Flask Response 对象

文章目录 创建 Response 对象设置响应内容设置响应状态码设置响应头完整的示例拓展设置响应的 cookie重定向响应发送文件作为响应 总结 Flask 是一个 Python Web 框架&#xff0c;用于快速开发 Web 应用程序。在 Flask 中&#xff0c;我们使用 Response 对象来构建 HTTP 响应。…...

算法001:移动零

力扣&#xff08;LeetCode&#xff09;. - 备战技术面试&#xff1f;力扣提供海量技术面试资源&#xff0c;帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/move-zeroes/ 使用 双指针 来解题&#xff1a; 此处的双指针&#xff0c;…...

基于springboot+vue+Mysql的网上书城管理系统

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…...

python实现绘制烟花代码

在Python中&#xff0c;我们可以使用多个库来绘制烟花效果&#xff0c;例如turtle库用于简单的绘图&#xff0c;或者更复杂的库如pygame或matplotlib结合动画。但是&#xff0c;由于turtle库是Python自带的&#xff0c;我们可以使用它来绘制一个简单的烟花效果。 下面是一个使…...

Python小白的机器学习入门指南

Python小白的机器学习入门指南 大家好&#xff01;今天我们来聊一聊如何使用Python进行机器学习。本文将为大家介绍一些基本的Python命令&#xff0c;并结合一个简单的数据集进行实例讲解&#xff0c;希望能帮助你快速入门机器学习。 数据集介绍 我们将使用一个简单的鸢尾花数…...

学校上课,是耽误我学习了。。

>>上一篇&#xff08;文科生在三本院校&#xff0c;读计算机专业&#xff09; 2015年9月&#xff0c;我入学了。 我期待的大学生活是多姿多彩的&#xff0c;我会参加各种社团&#xff0c;参与各种有意思的活动。 但我是个社恐&#xff0c;有过尝试&#xff0c;但还是难…...

OpenFeign高级用法:缓存、QueryMap、MatrixVariable、CollectionFormat优雅地远程调用

码到三十五 &#xff1a; 个人主页 微服务架构中&#xff0c;服务之间的通信变得尤为关键。OpenFeign&#xff0c;一个声明式的Web服务客户端&#xff0c;使得REST API的调用变得更加简单和优雅。OpenFeign集成了Ribbon和Hystrix&#xff0c;具有负载均衡和容错的能力&#xff…...

python基础之函数

目录 1.函数相关术语 2.函数类型分类 3.栈 4.位置参数和关键字参数 5.默认参数 6.局部变量和全局变量 7.返回多个值 8.怀孕函数 9.匿名函数 10.可传递任意个数实参的函数 11.函数地址与函数接口 12.内置函数修改与函数包装 1.函数相关术语 函数的基本概念有函数头…...

深入理解C#中的IO操作 - FileStream流详解与示例

文章目录 一、FileStream类的介绍二、文件读取和写入2.1 文件读取&#xff08;FileStream.Read&#xff09;2.2 文件写入&#xff08;FileStream.Write&#xff09; 三、文件复制、移动和目录操作3.1 文件复制&#xff08;FileStream.Copy&#xff09;3.2 文件移动&#xff08;…...

信息泄露--注意点点

目录 明确目标: 信息泄露: 版本软件 敏感文件 配置错误 url基于文件: url基于路由: 状态码: http头信息泄露 报错信息泄露 页面信息泄露 robots.txt敏感信息泄露 .get文件泄露 --判断: 搜索引擎收录泄露 BP: 爆破: 明确目标: 失能 读取 写入 执行 信息泄…...

位运算符

自学python如何成为大佬(目录):https://blog.csdn.net/weixin_67859959/article/details/139049996?spm1001.2014.3001.5501 位运算符是把数字看作二进制数来进行计算的&#xff0c;因此&#xff0c;需要先将要执行运算的数据转换为二进制&#xff0c;然后才能进行执行运算。…...

云上聚智——移动云云服务器进行后端的搭建及部署

什么是移动云 移动云是指将移动设备和云计算技术相结合&#xff0c;为移动应用提供强大的计算和存储能力的服务模式。传统的移动应用通常在本地设备上进行计算和存储&#xff0c;而移动云将这些任务转移到云端进行处理。通过移动云&#xff0c;移动设备可以利用云端的高性能计算…...

C语言程序的编译

目录 一、预处理&#xff08;预编译&#xff09; 二、编译 三、汇编 四&#xff0c;链接 在前面讲到了宏的定义&#xff0c;那么宏在编译时候是如何发生替换的&#xff1f;接下来做一下详细的介绍C语言程序的编译过程主要包括以下几个步骤&#xff1a;预处理、编译、汇编和…...

滴滴三面 | Go后端研发

狠狠的被鞭打了快两个小时… 注意我写的题解不一定是对的&#xff0c;如果你认为有其他答案欢迎评论区留言 bg&#xff1a;23届 211本 社招 1. 自我介绍 2. 讲一个项目的点&#xff0c;因为用到了中间件平台的数据同步&#xff0c;于是开始鞭打数据同步。。 3. 如果同步的时候…...

深度学习之基于Yolov3的行人重识别

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 一、项目背景 行人重识别&#xff08;Person Re-Identification&#xff0c;简称ReID&#xff09;是计算机视觉领域…...

防火墙最新技术汇总

防火墙技术持续发展&#xff0c;以应对日益复杂的网络安全威胁。以下是防火墙领域的一些最新技术汇总&#xff1a; 下一代防火墙&#xff08;NGFW&#xff09;&#xff1a;NGFW结合了传统防火墙的分组过滤和状态检测功能&#xff0c;还集成了深度包检测&#xff08;DPI&#xf…...

PikaUnsafe upfileupload

1.client check 客户端检测&#xff0c;前端js检测&#xff0c;禁用js和修改后缀名即可。 php格式不能上传&#xff0c;我们修改后缀上传。 蚁剑成功连接。 2.MIME type 这个就是 content-type 规定上传类型&#xff0c;上面的方法也能成功&#xff0c;也可以修改 conten-ty…...

[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解

突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 ​安全措施依赖问题​ GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...

页面渲染流程与性能优化

页面渲染流程与性能优化详解&#xff08;完整版&#xff09; 一、现代浏览器渲染流程&#xff08;详细说明&#xff09; 1. 构建DOM树 浏览器接收到HTML文档后&#xff0c;会逐步解析并构建DOM&#xff08;Document Object Model&#xff09;树。具体过程如下&#xff1a; (…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

DeepSeek 技术赋能无人农场协同作业:用 AI 重构农田管理 “神经网”

目录 一、引言二、DeepSeek 技术大揭秘2.1 核心架构解析2.2 关键技术剖析 三、智能农业无人农场协同作业现状3.1 发展现状概述3.2 协同作业模式介绍 四、DeepSeek 的 “农场奇妙游”4.1 数据处理与分析4.2 作物生长监测与预测4.3 病虫害防治4.4 农机协同作业调度 五、实际案例大…...

基于matlab策略迭代和值迭代法的动态规划

经典的基于策略迭代和值迭代法的动态规划matlab代码&#xff0c;实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...

Web 架构之 CDN 加速原理与落地实践

文章目录 一、思维导图二、正文内容&#xff08;一&#xff09;CDN 基础概念1. 定义2. 组成部分 &#xff08;二&#xff09;CDN 加速原理1. 请求路由2. 内容缓存3. 内容更新 &#xff08;三&#xff09;CDN 落地实践1. 选择 CDN 服务商2. 配置 CDN3. 集成到 Web 架构 &#xf…...

安卓基础(aar)

重新设置java21的环境&#xff0c;临时设置 $env:JAVA_HOME "D:\Android Studio\jbr" 查看当前环境变量 JAVA_HOME 的值 echo $env:JAVA_HOME 构建ARR文件 ./gradlew :private-lib:assembleRelease 目录是这样的&#xff1a; MyApp/ ├── app/ …...

20个超级好用的 CSS 动画库

分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码&#xff0c;而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库&#xff0c;可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画&#xff0c;可以包含在你的网页或应用项目中。 3.An…...

Windows电脑能装鸿蒙吗_Windows电脑体验鸿蒙电脑操作系统教程

鸿蒙电脑版操作系统来了&#xff0c;很多小伙伴想体验鸿蒙电脑版操作系统&#xff0c;可惜&#xff0c;鸿蒙系统并不支持你正在使用的传统的电脑来安装。不过可以通过可以使用华为官方提供的虚拟机&#xff0c;来体验大家心心念念的鸿蒙系统啦&#xff01;注意&#xff1a;虚拟…...

Linux操作系统共享Windows操作系统的文件

目录 一、共享文件 二、挂载 一、共享文件 点击虚拟机选项-设置 点击选项&#xff0c;设置文件夹共享为总是启用&#xff0c;点击添加&#xff0c;可添加需要共享的文件夹 查询是否共享成功 ls /mnt/hgfs 如果显示Download&#xff08;这是我共享的文件夹&#xff09;&…...