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

Flutter sqflite插件

数据库插件

dependencies:sqflite: ^2.4.2

请添加图片描述

class MyState extends State {String dbPath = "";Database? database;///获取数据库路径void getDBPath() {Future<String> path = getDatabasesPath();path.then((value) {dbPath = value;print(value);});}void initDB() {//如果没有word数据库,则会调用onCreate方法创建数据库。Future<Database> db = openDatabase(version: 2,"$dbPath/word.db",onCreate: (db, version) {String createSql ='CREATE TABLE "word" ("id" integer PRIMARY KEY AUTOINCREMENT,"def" text,"pron" text,"word" text)';db.execute(createSql);},onUpgrade: (db, oldVersion, newVersion) {//当前版本大于原版本,则执行if (newVersion >= 2) {db.execute("ALTER TABLE word ADD COLUMN collect integer");}print("$oldVersion,$newVersion");},);db.then((value) {database = value;print(value);});}Widget build(BuildContext context) {return DefaultTabController(length: 5,child: Scaffold(appBar: AppBar(title: Text("购物")),body: Column(children: [OutlinedButton(onPressed: () {getDBPath();},child: Text("获取数据库路径"),),OutlinedButton(onPressed: () {initDB();},child: Text("初始化数据库"),),OutlinedButton(onPressed: () {if (database == null) {initDB();} else {Future<int>? add = database?.rawInsert('INSERT INTO word(id, def, pron, word) VALUES (?,?,?,?)',[2, "抛弃;放纵", " əˈbændənmənt", "abandonment"],);add?.then((value) {print("添加数据的id:$value");});}},child: Text("插入数据"),),OutlinedButton(onPressed: () {if (database == null) {initDB();} else {Future<int>? add = database?.rawUpdate('update word set collect = ? where id = ?',[1, 2],);add?.then((value) {print("修改了$value条");});}},child: Text("修改数据"),),OutlinedButton(onPressed: () {if (database == null) {initDB();} else {Future<List<Map<String, Object?>>>? qu = database?.rawQuery('select * from word where id = ?',[2],);qu?.then((value) {print("查询数据:$value");});}},child: Text("查询数据"),),OutlinedButton(onPressed: () {if (database == null) {initDB();} else {Future<int>? del = database?.rawDelete('delete from word where id = ?',[2],);del?.then((value) {print("删除了$value条");});}},child: Text("删除数据"),),],),),);}
}

直接使用db文件的情况

    File file = File("$path/school.db");rootBundle.load("assert/db/school.db").then((value) {file.writeAsBytes(value.buffer.asUint8List());},);Future<Database> db = openDatabas("$path/school.db");
http://www.xdnf.cn/news/17848.html

相关文章:

  • Docker运行python项目:使用Docker成功启动FastAPI应用
  • Java 中导出 Excel 文件的方法
  • 本地jar导入到本地仓科和远程仓库
  • [ HTML 前端 ] 语法介绍和HBuilderX安装
  • Spring Boot 3中JWT密钥安全存储方案
  • 图灵测试:人工智能的“行为主义判据”与哲学争议
  • 论,物联网日志系统架构如何设计?
  • 使用colmap自制3DGaussian_Splatting数据集
  • Java进阶学习之Stream流的基本概念以及使用技巧
  • 第四天~在CANFD或CAN2.0的ARXML文件中实现Multiplexor多路复用信号实战
  • 3D-R1、Scene-R1、SpaceR论文解读
  • Codeforces Round 1042 (Div. 3)
  • Ansys FreeFlow入门:对搅拌罐进行建模
  • vector 认识及使用
  • 【论文阅读-Part1】PIKE-RAG: sPecIalized KnowledgE and Rationale Augmented Generation
  • 如何通过WiFi将文件从安卓设备传输到电脑
  • Scrapy 基础框架搭建教程:从环境配置到爬虫实现(附实例)
  • Pytorch在FSDP模型中使用EMA
  • 考研408《计算机组成原理》复习笔记,第四章(3)——指令集、汇编语言
  • 14、C 语言联合体和枚举知识点总结
  • Linux系统Namespace隔离实战:dd/mkfs/mount/unshare命令组合应用
  • 报数游戏(我将每文更新tips)
  • 2022 年全国硕士研究生招生考试真题笔记
  • 杂记 01
  • elasticsearch基础概念与集群部署
  • Blender模拟结构光3D Scanner(一)外参数匹配
  • ARM芯片架构之CoreSight Channel Interface 介绍
  • 20250813测试开发岗(凉)面
  • Spring Security 前后端分离场景下的会话并发管理
  • 商品分类拖拽排序设计