当前位置: 首页 > 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.dtcms.com/a/330559.html

相关文章:

  • Day57--图论--53. 寻宝(卡码网)
  • Nacos-4--Nacos1.x长轮询的理解
  • PiscTrace基于YOLO追踪算法的物体速度检测系统详解
  • 【软考中级网络工程师】知识点之入侵防御系统:筑牢网络安全防线
  • 【入门级-算法-2、入门算法:模拟法】
  • 解决“Win7共享文件夹其他电脑网络无法发现共享电脑名称”的问题
  • 融合服务器助力下的电视信息发布直播点播系统革新
  • 服务器装两个cpu
  • 1780. 判断一个数字是否可以表示成三的幂的和
  • MongoDB 从入门到生产:建模、索引、聚合、事务、分片与运维实战(含 Node.js/Python 示例)
  • 基于现代 C++ 的湍流直接数值模拟 (DNS) 并行算法优化与实现
  • 9.【C++进阶】继承
  • 河南萌新联赛2025第(五)场:信息工程大学”(补题)
  • QLab Pro for Mac —— 专业现场音频与多媒体控制软件
  • Boost库中Pool 基础内存池(boost::pool<>)的详细用法解析和实战应用
  • filezilla mac新版本MacOS-12.6.3会自动进入全屏模式BUG解决方法
  • 图论Day2学习心得
  • 支持pcm语音文件缓存顺序播放
  • springBoot+knife4j+openapi3依赖问题参考
  • 图灵测试:人工智能的“行为主义判据”与哲学争议
  • 计算机毕设大数据选题推荐 基于spark+Hadoop+python的贵州茅台股票数据分析系统【源码+文档+调试】
  • 浏览器环境下AES-GCM JavaScript 加解密程序
  • Vue Vant应用-数据懒加载
  • Docker + Cronicle + Traefik 搭建服务器计划任务工具
  • Vue3从入门到精通: 4.2 Vue Router路由守卫与权限控制深度解析
  • uni-app 小程序跳转小程序
  • rn入口文件setup.js解读
  • JS 解构赋值语法
  • 问题总结三
  • 最新去水印小程序系统 前端+后端全套源码 多套模版 免授权(源码下载)