Flutter - 集成三方库:数据库(sqflite)

数据库

$ flutter pub add sqlite
$ flutter pub get
$ flutter run

运行失败,看是编译报错,打开Xcode工程 ⌘ + B 编译

2025-05-15 08.47.33.png

对比 GSYGithubAppFlutter 的Xcode工程Build Phases > [CP] Embed Pods Frameworks 有sqfite.framework。本地默认的Flutter工程默认未生成Podfile

2025-05-15 19.03.14.png

然后查看 GSYGithubAppFlutter

...
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)flutter_ios_podfile_setuptarget 'Runner' douse_frameworks!use_modular_headers!flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
...

看代码是引入了Flutter提供的工具的,从flutter的安装目录下找到podhelper.rb这个文件

17473073456022.jpg

# 方法: flutter_install_all_ios_pods
# 安装Flutter在iOS平台上的引擎和插件
def flutter_install_all_ios_pods(ios_application_path = nil)# 创建Flutter引擎的.podspec文件flutter_install_ios_engine_pod(ios_application_path)flutter_install_plugin_pods(ios_application_path, '.symlinks', 'ios')
end
# 方法: flutter_install_plugin_pods
def flutter_install_plugin_pods(application_path = nil, relative_symlink_dir, platform)# CocoaPods定义了 defined_in_file,获取应用路径,未获取到就中断application_path ||= File.dirname(defined_in_file.realpath) if respond_to?(:defined_in_file)raise 'Could not find application path' unless application_path# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock# referring to absolute paths on developers' machines.# 使用符号链接,避免使用Podfile.lock这个文件# Flutter是在ios目录下创建.symlinks目录,里面有软链接指向Flutter下载包的位置,这样只需要一份即可。# 先删除,再创建对应的目录symlink_dir = File.expand_path(relative_symlink_dir, application_path)system('rm', '-rf', symlink_dir) symlink_plugins_dir = File.expand_path('plugins', symlink_dir)system('mkdir', '-p', symlink_plugins_dir)plugins_file = File.join(application_path, '..', '.flutter-plugins-dependencies')dependencies_hash = flutter_parse_plugins_file(plugins_file)plugin_pods = flutter_get_plugins_list(dependencies_hash, platform)swift_package_manager_enabled = flutter_get_swift_package_manager_enabled(dependencies_hash, platform)plugin_pods.each do |plugin_hash|plugin_name = plugin_hash['name']plugin_path = plugin_hash['path']...# 使用path: 的方式本地依赖需要的三方库# 手动添加打印确认下# print "plugin_name:#{plugin_name}\n"pod plugin_name, path: File.join(relative, platform_directory)end
end
$ pod update --verbose

2025-05-15 19.33.17.png

因此Podfile里的target部分就依赖了sqflite_darwin

target 'Runner' douse_frameworks!use_modular_headers!...pod 'sqflite_darwin', path:.symlinks/plugins/sqflite_darwin/darwin
end

2025-05-15 19.34.50.png

使用

打开/关闭/删除数据库
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'finger.db');/// 打开数据库
Database database = await openDatabase(path, version: 1,onCreate: (Database db, int version) async {/// 当创建数据库时创建tableawait db.execute('CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
});
/// 关闭数据库
await db.close();
/// 删除数据库
await deleteDatabase(path);
/// 添加表
await database.execute("CREATE TABLE Test2(id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)",);/// 删除表
await database.execute('DROP TABLE Test2');
使用SQL语句
/// 添加数据
await database.transaction((txn) async {int id1 = await txn.rawInsert('INSERT INTO Test(name, value, num) VALUES("some name", 1234, 456.789)');int id2 = await txn.rawInsert('INSERT INTO Test(name, value, num) VALUES(?, ?, ?)',['another name', 12345678, 3.1416]);
});
/// 删除数据
count = await database.rawDelete('DELETE FROM Test WHERE name = ?', ['another name']);
/// 更新数据
int count = await database.rawUpdate('UPDATE Test SET name = ?, value = ? WHERE name = ?',['updated name', '9876', 'some name']);
/// 查询数据
List<Map> list = await database.rawQuery('SELECT * FROM Test');
print(list)

2025-05-17 20.31.58.png

使用工具方法

使用Sqflite提供的工具方法来执行数据库操作,而不是直接使用SQL语句

import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';final String tName = 'company';
final String columnId = "_id";
final String columnName = "name";class Company {int? id;String? name;Company();Map<String, Object?> toMap() {var map = <String, Object?>{columnName: name};if (id != null) {map[columnId] = id;}return map;}Company.fromMap(Map map) {id = map[columnId];name = map[columnName];}
}class CompanyProvider {Database? db;Future<Database?> open() async {if (db == null) {var databasesPath = await getDatabasesPath();String path = join(databasesPath, 'demo.db');db = await openDatabase(path,version: 1,onCreate: (Database db, int version) async {await db.execute('''create table $tName ($columnId integer primary key autoincrement,$columnName text not null)''');},);}return db;}/// 注册企业Future insert(Company company) async {/// 工具方法: 传表名 + 列信息添加数据到数据库company.id = await db?.insert(tName, company.toMap());return company;}/// 查找企业Future findById(int id) async {List<Map> maps = await db!.query(tName, /// 表名columns: [columnId, columnName], /// 查找的列where: '$columnId = ?', /// 查找条件whereArgs: [id], /// 每个问号填充的值);if (maps.isNotEmpty) {return Company.fromMap(maps.first);}return null;}/// 查找所有的企业Future<List<Company>> find() async {List<Company> companys = [];List<Map> maps = await db!.query(tName, columns: [columnId, columnName]);for (var map in maps) {Company c = Company.fromMap(map);companys.add(c);}return companys;}/// 删除企业Future delete(int id) async {/// 根据id列删除企业return await db?.delete(tName, where: '$columnId = ?', whereArgs: [id]);}/// 更新企业信息Future update(Company company) async {return await db?.update(tName,company.toMap(),where: '$columnId = ?',whereArgs: [company.id],);}
}
void test() async {/// 添加2条测试数据CompanyProvider cp = CompanyProvider();await cp.open();List<Map> maps = [{"name": "Google"},{"name": "Apple"},];/// 新增数据int firstId = 0;for (int i = 0; i < maps.length; ++i) {Company c = Company.fromMap(maps[i]);cp.insert(c);}/// 查找数据List<Company> companys = await cp.find();if (companys.isNotEmpty) {firstId = companys.first.id!;}if (firstId > 0) {Company firstCompany = await cp.findById(firstId);print(firstCompany.toMap());/// 更新数据Company chgCompany = Company();chgCompany.id = firstId;chgCompany.name = DateTime.now().microsecondsSinceEpoch.toString();cp.update(chgCompany);firstCompany = await cp.findById(firstId);print(firstCompany.toMap());/// 删除数据cp.delete(firstId);}}

2025-05-20 15.33.50.png

数据库迁移

随着功能迭代,需要对数据库的表结构进行修改时,比如增加新字段时,需要对表的结构进行更新。

Future<Database?> open() async {if (db == null) {var databasesPath = await getDatabasesPath();String path = join(databasesPath, 'demo.db');db = await openDatabase(path,version: 2,/// 1.新版本发布时改成2onCreate: (db, version) async {/// 2.新安装设备触发onCreate,所以这里添加新的字段await db.execute('''create table $tName ($columnId integer primary key autoincrement,$columnName text not null,$columnDesc text)''');},onUpgrade: (db, oldVersion, newVersion) async {var batch = db.batch();/// [onUpgrade] is called if either of /// the following conditions are met:/// 1. [onCreate] is not specified/// 2. The database already exists and [version] is higher than the last database version/// onUpgrade回调在未指定onCreate回调或者数据库已经存在同时version字段高于已安装的版本,执行完onUpgrade回调后应该会更新关联的版本,设置断点让onUpgrade执行中断,下次还会会执行这个方法/// 3.对旧版本的设备:判断安装设备已创建的数据库版本if (oldVersion == 1) {_updateTableCompanyV1toV2(batch);}await batch.commit();},);}return db;}
/// 4.添加description字段
void _updateTableCompanyV1toV2(Batch batch) {batch.execute('ALTER TABLE Company ADD description TEXT');
}/// 其它的一些处理
final String columnDesc = "description";
...class Company {int? id;String? name;/// 5.模型增加对应字段 + 列String? description;.../// 6. 更新map和对象的转换方法Map<String, Object?> toMap() {var map = <String, Object?>{columnName: name, columnDesc: description};if (id != null) {...
/// 调用
...
firstCompany.description = "版本2新增的字段";
print(firstCompany.toMap());

2025-05-20 16.26.51.png

事务

数据库的增删改查可能会失败,导致数据与预期的不一致,为了保证在执行前后的数据一致性,引入了事务。事务具有ACID这4个特性:原子性、一致性、隔离性和持久性。

在事务中不要使用数据库,而只需要使用事务对象访问数据库。

await database.transaction((txn) async {// 正确await txn.execute('CREATE TABLE Test1 (id INTEGER PRIMARY KEY)');// 不要在事务中使用数据库// 下面会导致死锁await database.execute('CREATE TABLE Test2 (id INTEGER PRIMARY KEY)');
});
try {await database.transaction((txn) async {await txn.update('TABLE', {'foo': 'bar'});});// No error, the transaction is committed// 1. 未报错,则事务被提交// cancel the transaction (any error will do)// 2. 取消或执行时报错,则抛出异常在,catch中被捕获// throw StateError('cancel transaction');
} catch (e, st) {// this reliably catch if there is a key conflict// We know that the transaction is rolled back.// 3. 事务被回滚,执行业务相关的操作,比如提示报错
}
批处理

使用 Batch,即批处理,来避免在 Dart 和原生代码之间的反复切换。

batch = db.batch();
batch.insert('Test', {'name': 'item'});
batch.update('Test', {'name': 'new_item'}, where: 'name = ?', whereArgs: ['item']);
batch.delete('Test', where: 'name = ?', whereArgs: ['item']);
/// 批处理统一提交
results = await batch.commit();

在事务中,批处理的commit会等到事务提交后

await database.transaction((txn) async {var batch = txn.batch();// ...// commit but the actual commit will happen when the transaction is committed// however the data is available in this transaction/// 当事务被提交时才会真正的提交await batch.commit();//  ...
});
/// 设置批处理出现错误依然提交
await batch.commit(continueOnError: true);
表名和列名

SQLite的关键词,要避免使用作为实体(Entity)名。

"add","all","alter","and","as","autoincrement","between","case","check","collate","commit","constraint","create","default","deferrable","delete","distinct","drop","else","escape","except","exists","foreign","from","group","having","if","in","index","insert","intersect","into","is","isnull","join","limit","not","notnull","null","on","or","order","primary","references","select","set","table","then","to","transaction","union","unique","update","using","values","when","where"

sqflite的工具方法会进行处理,避免与关键字的冲突

db.query('table')
/// 等价于
db.rawQuery('SELECT * FROM "table"');

其它问题

VSCode 无法调试

Error connecting to the service protocol: failed to connect to http://127.0.0.1:51020/Kra7fZnYjeI=/ Error: Failed to register service methods on attached VM Service: registerService: (-32000) Service connection disposed

原来有成功过,后面发现一直都会有问题,前段时间突然不行,在长时间运行后就会报这个错误,但是单独在VSCode外部用flutter run命令能正常运行。

发现终端可以是把本地的端口转发的代理给去掉了。然后发现VSCode的代理有这样的说明,若未设置则会继承环境变量中的http_proxyhttps_proxy,我把代理加到.zshrc中,所以VSCode的默认会用代理,但是运行在真机上,手机没有代理,应该是这样影响了网络环境。

  1. .zshrc去掉代理的配置
  2. 重新打开VSCode && 运行 => 能正常调试

2025-05-15 18.54.02.png

参考

  1. SQLite CRUD operations in Flutter
  2. sqflite-doc
  3. sqflite Migration example

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/83961.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android 中 权限分类及申请方式

在 Android 中,权限被分为几个不同的类别,每个类别有不同的申请和管理方式。 一、 普通权限(Normal Permissions) 普通权限通常不会对用户隐私或设备安全造成太大风险。这些权限在应用安装时自动授予,无需用户在运行时手动授权。 android.permission.INTERNETandroid.pe…

目标检测指标计算

mAP&#xff08;mean Average Precision&#xff09; 概述 预备参数&#xff1a;类别数&#xff0c;IoU阈值&#xff0c;maxDets值&#xff08;每张测试图像最多保留maxDets个预测框&#xff0c;通常是根据置信度得分排序后取前maxDets个&#xff09;&#xff1b; Q: 假如某张…

联合索引失效情况分析

一.模拟表结构&#xff1a; 背景&#xff1a; MySQL版本——8.0.37 表结构DDL&#xff1a; CREATE TABLE unite_index_table (id bigint NOT NULL AUTO_INCREMENT COMMENT 主键,clomn_first varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMEN…

软件架构之-论分布式架构设计及其实现

论分布式架构设计及其实现 摘要正文摘要 2023年2月,本人所在集团公司承接了长三角地区某省渔船图纸电子化审查项目开发,该项目旨在为长三角地区渔船建造设计院、渔船审图机构提供一个便捷化的服务平台。在次项目中,我作为项目成员参与了整个项目的建设工作,全权负责项目需求…

Pydantic数据验证实战指南:让Python应用更健壮与智能

导读&#xff1a;在日益复杂的数据驱动开发环境中&#xff0c;如何高效、安全地处理和验证数据成为每位Python开发者面临的关键挑战。本文全面解析了Pydantic这一革命性数据验证库&#xff0c;展示了它如何通过声明式API和类型提示系统&#xff0c;彻底改变Python数据处理模式。…

3、ubantu系统 | 通过vscode远程安装并配置anaconda

1、vscode登录 登录后通过pwd可以发现目前位于wangqinag账号下&#xff0c;左侧为属于该账号的文件夹及文件。 通过cd ..可以回到上一级目录&#xff0c;通过ls可以查看当前目录下的文件夹及文件。 2、安装 2.1、下载anaconda 通过wget和curl下载未成功&#xff0c;使用手动…

Python 与 Java 在 Web 开发中的深度对比:从语言特性到生态选型

在 Web 开发领域&#xff0c;Python 和 Java 作为两大主流技术栈&#xff0c;始终是开发者技术选型时的核心考量。本文将从语言本质、框架生态、性能工程、工程实践等多个维度展开深度对比&#xff0c;结合具体技术场景解析两者的适用边界与融合方案&#xff0c;为开发者提供系…

【OpenGL学习】(一)创建窗口

文章目录 【OpenGL学习】&#xff08;一&#xff09;创建窗口 【OpenGL学习】&#xff08;一&#xff09;创建窗口 GLFW OpenGL 本身只是一套图形渲染 API&#xff0c;不提供窗口创建、上下文管理或输入处理的功能。 GLFW 是一个支持创建窗口、处理键盘鼠标输入和管理 OpenGL…

电脑闪屏可能的原因

1. 显示器 / 屏幕故障 屏幕排线接触不良&#xff1a;笔记本电脑屏幕排线&#xff08;屏线&#xff09;松动或磨损&#xff0c;导致信号传输不稳定&#xff0c;常见于频繁开合屏幕的设备。屏幕面板损坏&#xff1a;液晶屏内部灯管老化、背光模块故障或面板本身损坏&#xff0c;…

docker容器知识

一、docker与docker compose区别&#xff1a; 1、docker是创建和管理单个容器的工具&#xff0c;适合简单的应用或服务&#xff1b; 2、docker compose是管理多容器应用的工具&#xff0c;适合复杂的、多服务的应用程序&#xff1b; 3、docker与docker compose对比&#xff…

什么是Rootfs

Rootfs (Root Filesystem) 详解 buildroot工具构建了一个名为"rootfs.tar"的根文件系统压缩包。 什么是rootfs Rootfs&#xff08;Root Filesystem&#xff0c;根文件系统&#xff09;是操作系统启动后挂载的第一个文件系统&#xff0c;它包含系统正常运行所需的基…

关于NLP自然语言处理的简单总结

参考&#xff1a; 什么是自然语言处理&#xff1f;看这篇文章就够了&#xff01; - 知乎 (zhihu.com) 所谓自然语言理解&#xff0c;就是研究如何让机器能够理解我们人类的语言并给出一些回应。 自然语言处理&#xff08;Natural Language Processing&#xff0c;NLP&#xff0…

Linux下载国外软件镜像的加速方法(以下载Python-3.8.0.tgz为例)

0 前言 使用linux经常会通过国外服务器下载软件镜像&#xff0c;有些软件的下载速度奇慢&#xff0c;本文介绍一种加速国外软件镜像下载速度的方法&#xff0c;需要准备下载工具&#xff1a;迅雷。 1 以下载Python-3.8.0.tgz为例 找到Python官网的Python-3.8.0.tgz镜像下载地…

没有公网ip怎么端口映射外网访问?使用内网穿透可以解决

无公网IP时本地搭建的网络端口服务怎么映射外网远程访问&#xff1f;较为简单通用的方案就是使用nat123内网穿透&#xff0c;下面详细内网映射外网实现教程。​ 一、了解内网公网区别&#xff0c;及无公网IP外网访问方案 内网IP默认只能在同局域网内连接互通&#xff0c;而公…

Word2Vec详解

目录 Word2Vec 一、Word2Vec 模型架构 &#xff08;一&#xff09;Word2Vec 的核心理念 &#xff08;二&#xff09;Word2Vec 的两种架构 &#xff08;三&#xff09;负采样与层次 Softmax &#xff08;四&#xff09;Word2Vec 的优势与局限 二、Word2Vec 预训练及数据集…

ShardingSphere:查询报错:Actual table `数据源名称.表名` is not in table rule configuration

目录 简介异常信息排查原因解决 简介 1、使用ShardingSphere框架&#xff0c;版本为5.2.1 <dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core</artifactId><version>5.2.1</version>…

MongoDB聚合查询:从入门到精通

文章目录 前言一、工具一般聚合查询分为四步 二、使用步骤1.MongoDB Compass2.Studio 3T 二、举个栗子总结 前言 Mongo 聚合查询 一般用mongo做数据库,涉及到关联查询情况不多,但是还有些情况要使用到,今天就讲下如何通过工具做关联查询,最终聚合结果,得到最终的查询结果集; …

codeup添加流水线docker自动化部署

在项目根目录下增加Dockerfile文件 # 使用基础镜像 FROM maven:3.8.4-openjdk-17-slim AS build # 设置工作目录 WORKDIR /app # 复制项目源代码 COPY . . # 构建项目 RUN mvn clean package -DskipTests # 验证JAR包是否生成 RUN ls -l target/your-project.jar # 使用合适的…

从 Word2Vec 到 BERT:AI 不止是词向量,更是语言理解

一、前言 在上篇文章中&#xff0c;我们介绍了Word2Vec以及它的作用&#xff0c;总的来说&#xff1a; Word2Vec是我们理解NLP的第一站 Word2Vec将词变成了“向量”—— 终于可以用机器理解词语的相似度 我们获得了例如“国王 - 男人 女人 ≈ 女王” 的类比能力 我们可以将…

镜像管理(2)Dockerfile总结

一、docker镜像构建方法 commoit :使用 docker commit 意味着所有对镜像的操作都是黑箱操作,生成的镜像也被称为黑 箱镜像,换句话说,就是除了制作镜像的人知道执行过什么命令、怎么生成的镜像,别人根 本无从得知。而且,即使是这个制作镜像的人,过一段时间后也无法记清具…