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

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.lryc.cn/news/620733.html

相关文章:

  • 支付域——账户系统设计
  • 支持pcm语音文件缓存顺序播放
  • 解剖HashMap的put <四> jdk1.8
  • OpenCv(二)——边界填充、阈值处理
  • Nacos 配置热更新:Spring Boot Bean 自动获取最新配置
  • flutter3.7.12版本设置TextField的contextMenuBuilder的文字颜色
  • MixOne在macOS上安装碰到的问题
  • 解决SQL Server连接失败:Connection refused: connect
  • 苹果正计划大举进军人工智能硬件领域
  • 从0开始跟小甲鱼C语言视频使用linux一步步学习C语言(持续更新)8.14
  • 2025 电赛 C 题 发挥3 带数字编号的正方形识别 边长测量
  • [特殊字符]走进华为,解锁商业传奇密码
  • 通过网页调用身份证阅读器http websocket方法-湖南步联科技美萍MP999A电子————仙盟创梦IDE
  • 从根源到生态:Apache Doris 与 StarRocks 的深度对比 —— 论开源基因与长期价值的优越性
  • 审批流程系统设计与实现:状态驱动、灵活扩展的企业级解决方案
  • 实战指南|消防管理系统搭建全流程解析
  • OpenCV ------图像基础处理(一)
  • 【P81 10-7】OpenCV Python【实战项目】——车辆识别、车流统计(图像/视频加载、图像运算与处理、形态学、轮廓查找、车辆统计及显示)
  • 【OpenCV】Mat详解
  • 入门基础人工智能理论
  • 计算机视觉(opencv)实战二——图像边界扩展cv2.copyMakeBorder()
  • 论,物联网日志系统架构如何设计?
  • AI增强SEO关键词表现
  • Postman 平替 技术解析:架构优势与实战指南
  • 考研408《计算机组成原理》复习笔记,第五章(2)——CPU指令执行过程
  • 使用 Docker 部署 PostgreSQL
  • 考研408《计算机组成原理》复习笔记,第四章(3)——指令集、汇编语言
  • Java设计模式之《策略模式》
  • Effective C++ 条款41:理解隐式接口和编译期多态
  • 应用系统连达梦数据库报“服务器模式不匹配”的根源与修复方案