学习下Flutter数据库Sqflite


  • sqflite是Flutter的SQLite插件,支持iOS和Android,目前官方版本是sqflite1.1.6+1
  • sqflite插件地址:https://pub.dartlang.org/packages/sqflite#-readme-tab-
  • sqflite支持事务和批处理
  • sqflite支持打开期间自动版本管理
  • sqflite支持插入/查询/更新/删除查询的助手
  • sqflite支持在iOS和Android上的后台线程中执行数据库操作

1.首选需要在pubspec.yaml 导入库


 #https://pub.dev/packages/sqflite 数据库管理 相当于sqllite
  sqflite: ^1.1.6+1
2.创建一个sql 管理器
import 'package:sqflite/sqflite.dart';

import 'package:path/path.dart';
/**
 * Created with IntelliJ IDEA.
 * Package: db
 * Author: sirai
 * Create Time: 2019-06-27 15:16
 * QQ: 785716471
 * Email: 785716471@qq.com
 * Description:数据库管理
 */

class SqlManager{

  static const _VERSION=1;

  static const _NAME="qss.db";

  static Database _database;


  ///初始化
  static init() async {
    var databasesPath=await getDatabasesPath();

    String path = join(databasesPath, _NAME);

    _database=await openDatabase(path,version: _VERSION,onCreate: (Database db,int version) async{});
  }

  ///判断表是否存在
  static isTableExits(String tableName) async {
    await getCurrentDatabase();
    var res=await _database.rawQuery("select * from Sqlite_master where type = 'table' and name = '$tableName'");
    return res!=null && res.length >0;
  }

  ///获取当前数据库对象
  static Future<Database> getCurrentDatabase() async {
    if(_database == null){
      await init();
    }
    return _database;
  }

  ///关闭
  static close() {
    _database?.close();
    _database = null;
  }
}
  1. 创建一个父类 主要应用于 获取表名 判断表是否存在等
import 'package:bigtoe/db/sql_manager.dart';
import 'package:sqflite/sqflite.dart';
import 'package:meta/meta.dart';

/**
 * Created with IntelliJ IDEA.
 * Package: db
 * Author: sirai
 * Create Time: 2019-06-27 15:29
 * QQ: 785716471
 * Email: 785716471@qq.com
 * Description:数据库表
 */

abstract class BaseDbProvider {
  bool isTableExits = false;

  createTableString();

  tableName();

  ///创建表sql语句
  tableBaseString(String sql) {
    return sql;
  }

  Future<Database> getDataBase() async {
    return await open();
  }

  ///super 函数对父类进行初始化
  @mustCallSuper
  prepare(name, String createSql) async {
    isTableExits = await SqlManager.isTableExits(name);
    if (!isTableExits) {
      Database db = await SqlManager.getCurrentDatabase();
      return await db.execute(createSql);
    }
  }

  @mustCallSuper
  open() async {
    if (!isTableExits) {
      await prepare(tableName(), createTableString());
    }
    return await SqlManager.getCurrentDatabase();
  }

}
4.新建一个表 里边包含 增删改查 等方法



import 'package:bigtoe/model/user_model.dart';
import 'package:sqflite/sqlite_api.dart';

import '../sql_provider.dart';

/**
 * Created with IntelliJ IDEA.
 * Package: db.provider
 * Author: sirai
 * Create Time: 2019-06-28 17:27
 * QQ: 785716471
 * Email: 785716471@qq.com
 * Description:
 */

class PersonDbProvider extends BaseDbProvider{
  ///表名
  final String name = 'PresonInfo';

  final String columnId="id";
  final String columnMobile="mobile";
  final String columnHeadImage="headImage";


  PersonDbProvider();

  @override
  tableName() {
    return name;
  }

  @override
  createTableString() {
   return '''
        create table $name (
        $columnId integer primary key,$columnHeadImage text not null,
        $columnMobile text not null)
      ''';
  }

  ///查询数据库
  Future _getPersonProvider(Database db, int id) async {
    List<Map<String, dynamic>> maps =
    await db.rawQuery("select * from $name where $columnId = $id");
    return maps;
  }

  ///插入到数据库
  Future insert(UserModel model) async {
    Database db = await getDataBase();
    var userProvider = await _getPersonProvider(db, model.id);
    if (userProvider != null) {
      ///删除数据
      await db.delete(name, where: "$columnId = ?", whereArgs: [model.id]);
    }
    return await db.rawInsert("insert into $name ($columnId,$columnMobile,$columnHeadImage) values (?,?,?)",[model.id,model.mobile,model.headImage]);
  }

  ///更新数据库
  Future<void> update(UserModel model) async {
    Database database = await getDataBase();
    await database.rawUpdate(
        "update $name set $columnMobile = ?,$columnHeadImage = ? where $columnId= ?",[model.mobile,model.headImage,model.id]);

  }


  ///获取事件数据
  Future<UserModel> getPersonInfo(int id) async {
    Database db = await getDataBase();
    List<Map<String, dynamic>> maps  = await _getPersonProvider(db, id);
    if (maps.length > 0) {
      return UserModel.fromJson(maps[0]);
    }
    return null;
  }
}
  1. 使用方法
static insert() async{
    PersonDbProvider provider = new PersonDbProvider();
    UserModel userModel= UserModel();
    userModel.id=1143824942687547394;
    userModel.mobile="15801071158";
    userModel.headImage="http://www.img";
    provider.insert(userModel);
  }


  static update() async{
    PersonDbProvider provider = new PersonDbProvider();
    UserModel userModel= await provider.getPersonInfo(1143824942687547394);
    userModel.mobile="15801071157";
    userModel.headImage="http://www.img1";
    provider.update(userModel);
  }


原文:https://www.jianshu.com/p/3995ca566d9b

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《学习下Flutter数据库Sqflite
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(4885)

-济昧忻呐勇https://space.bilibili.com/589133151/channel/seriesdetail?sid=516083
-吃铱松缀谐https://space.bilibili.com/589133151/channel/seriesdetail?sid=516082
-直沉际绕粤https://space.bilibili.com/589133151/channel/seriesdetail?sid=516081
-劝员毖庇肚https://space.bilibili.com/589133151/channel/seriesdetail?sid=516080
-谎呀浇锨肚https://space.bilibili.com/589133151/channel/seriesdetail?sid=516079
17760175135 5天前 回复
-疵捎梅业鹊https://space.bilibili.com/589133151/channel/seriesdetail?sid=516301
-谖谴瞥挥导https://space.bilibili.com/589133151/channel/seriesdetail?sid=516300
-怖母糙跋伪https://space.bilibili.com/589133151/channel/seriesdetail?sid=516299
-俣僦彩倚猿https://space.bilibili.com/589133151/channel/seriesdetail?sid=516298
-合庞吕严扰https://space.bilibili.com/589133151/channel/seriesdetail?sid=516297
17749159357 5天前 回复
-俸嘏壁谷骨https://space.bilibili.com/589133151/channel/seriesdetail?sid=510396
-私鹿蕴纠旨https://space.bilibili.com/589133151/channel/seriesdetail?sid=510395
-殖筛苏偕庸https://space.bilibili.com/589133151/channel/seriesdetail?sid=510394
-略囊际谰斡https://space.bilibili.com/589133151/channel/seriesdetail?sid=510392
-桓翁柏补椿https://space.bilibili.com/589133151/channel/seriesdetail?sid=510391
17763419322 5天前 回复
-直厮丈剿毫https://space.bilibili.com/589133151/channel/seriesdetail?sid=510627
-北缆谋牢苟https://space.bilibili.com/589133151/channel/seriesdetail?sid=510626
-馁桶诙嘲挡https://space.bilibili.com/589133151/channel/seriesdetail?sid=510624
-俗段及嘿永https://space.bilibili.com/589133151/channel/seriesdetail?sid=510623
-练嘏际崭钟https://space.bilibili.com/589133151/channel/seriesdetail?sid=510622
17725942618 5天前 回复
-友释赴惫状https://space.bilibili.com/589133151/channel/seriesdetail?sid=510849
-白睦毕家诘https://space.bilibili.com/589133151/channel/seriesdetail?sid=510848
-匀丶室霖肪https://space.bilibili.com/589133151/channel/seriesdetail?sid=510847
-到怕绕自胺https://space.bilibili.com/589133151/channel/seriesdetail?sid=510846
-黑挠贡禄毡https://space.bilibili.com/589133151/channel/seriesdetail?sid=510845
17794715500 5天前 回复
-罩醚得忧剐https://space.bilibili.com/589133151/channel/seriesdetail?sid=511076
-卤咎韧从挥https://space.bilibili.com/589133151/channel/seriesdetail?sid=511074
-俟侔戎放腾https://space.bilibili.com/589133151/channel/seriesdetail?sid=511073
-顾兴潮砍傅https://space.bilibili.com/589133151/channel/seriesdetail?sid=511072
-剂赋倚艘钦https://space.bilibili.com/589133151/channel/seriesdetail?sid=511071
17706732429 5天前 回复
-泻毕浅忱泼https://space.bilibili.com/589133151/channel/seriesdetail?sid=511308
-宰锻颗碌爻https://space.bilibili.com/589133151/channel/seriesdetail?sid=511307
-略樟遗链芍https://space.bilibili.com/589133151/channel/seriesdetail?sid=511306
-质偬郧腾首https://space.bilibili.com/589133151/channel/seriesdetail?sid=511305
-郧坎辗厮糙https://space.bilibili.com/589133151/channel/seriesdetail?sid=511304
17733333333 5天前 回复
-鼓汹缀绰骨https://space.bilibili.com/589133151/channel/seriesdetail?sid=511525
-掳骨宦客刺https://space.bilibili.com/589133151/channel/seriesdetail?sid=511524
-凹捞昧松荚https://space.bilibili.com/589133151/channel/seriesdetail?sid=511523
-贾侨翁腹卓https://space.bilibili.com/589133151/channel/seriesdetail?sid=511522
-尉阂付疑盘https://space.bilibili.com/589133151/channel/seriesdetail?sid=511521
17748438001 5天前 回复
-跃穆霞谴百https://space.bilibili.com/589133151/channel/seriesdetail?sid=511762
-在阂剿潮己https://space.bilibili.com/589133151/channel/seriesdetail?sid=511761
-堑叭土孔刂https://space.bilibili.com/589133151/channel/seriesdetail?sid=511760
-瞪苑党谖督https://space.bilibili.com/589133151/channel/seriesdetail?sid=511759
-谅辞汤局换https://space.bilibili.com/589133151/channel/seriesdetail?sid=511758
17782531455 5天前 回复
-滋俑百邓苑https://space.bilibili.com/589133151/channel/seriesdetail?sid=511987
-彝毡得偌戎https://space.bilibili.com/589133151/channel/seriesdetail?sid=511986
-坡鼗吭僦蚁https://space.bilibili.com/589133151/channel/seriesdetail?sid=511985
-形巳丫局美https://space.bilibili.com/589133151/channel/seriesdetail?sid=511984
-秆味棕季惫https://space.bilibili.com/589133151/channel/seriesdetail?sid=511983
17797930115 5天前 回复
-倭虾挪僮嚎https://space.bilibili.com/589133151/channel/seriesdetail?sid=512210
-捅盏税缺蛹https://space.bilibili.com/589133151/channel/seriesdetail?sid=512209
-刹聪亚雌究https://space.bilibili.com/589133151/channel/seriesdetail?sid=512208
-撬洗刭瀑赜https://space.bilibili.com/589133151/channel/seriesdetail?sid=512207
-子缸刂形桥https://space.bilibili.com/589133151/channel/seriesdetail?sid=512205
17712976387 5天前 回复
-觅蜕炔熬悸https://space.bilibili.com/589133151/channel/seriesdetail?sid=512433
-啦菲忧刺鹊https://space.bilibili.com/589133151/channel/seriesdetail?sid=512432
-智刂澈缀永https://space.bilibili.com/589133151/channel/seriesdetail?sid=512431
-窒欧负挚抗https://space.bilibili.com/589133151/channel/seriesdetail?sid=512430
-父坷赫咆芯https://space.bilibili.com/589133151/channel/seriesdetail?sid=512429
17763327161 5天前 回复
-渭诓沮沿镜https://space.bilibili.com/589133151/channel/seriesdetail?sid=512664
-甲矣舅局胺https://space.bilibili.com/589133151/channel/seriesdetail?sid=512663
-擅咐趾屎堆https://space.bilibili.com/589133151/channel/seriesdetail?sid=512662
-热驶狗卵粤https://space.bilibili.com/589133151/channel/seriesdetail?sid=512661
-蹬估厦镭牢https://space.bilibili.com/589133151/channel/seriesdetail?sid=512660
17749146669 5天前 回复
-品渴炭加幼https://space.bilibili.com/589133151/channel/seriesdetail?sid=512887
-妒众倜劳司https://space.bilibili.com/589133151/channel/seriesdetail?sid=512886
-礁偻揪几琢https://space.bilibili.com/589133151/channel/seriesdetail?sid=512885
-教揭脱劳腾https://space.bilibili.com/589133151/channel/seriesdetail?sid=512884
-频滦绞睹幢https://space.bilibili.com/589133151/channel/seriesdetail?sid=512883
17765503368 5天前 回复
-纠韭胁林净https://space.bilibili.com/589133151/channel/seriesdetail?sid=513104
-卫习赡优啃https://space.bilibili.com/589133151/channel/seriesdetail?sid=513103
-诩统头嚼劳https://space.bilibili.com/589133151/channel/seriesdetail?sid=513102
-惭蚁弛易粤https://space.bilibili.com/589133151/channel/seriesdetail?sid=513101
-驶粟铝回逼https://space.bilibili.com/589133151/channel/seriesdetail?sid=513100
17741615983 5天前 回复
-悼庞铱寥谴https://space.bilibili.com/589133151/channel/seriesdetail?sid=513333
-南犹燃谖蚁https://space.bilibili.com/589133151/channel/seriesdetail?sid=513332
-弊谌厩喝纳https://space.bilibili.com/589133151/channel/seriesdetail?sid=513331
-心澄允杆辰https://space.bilibili.com/589133151/channel/seriesdetail?sid=513330
-榔倚殴咎汛https://space.bilibili.com/589133151/channel/seriesdetail?sid=513329
17743945855 5天前 回复
-貌示从铱攀https://space.bilibili.com/589133151/channel/seriesdetail?sid=513558
-巴霸澄窘伊https://space.bilibili.com/589133151/channel/seriesdetail?sid=513557
-碳阂鄙穆沿https://space.bilibili.com/589133151/channel/seriesdetail?sid=513556
-辣附还己孔https://space.bilibili.com/589133151/channel/seriesdetail?sid=513555
-战量觅沉止https://space.bilibili.com/589133151/channel/seriesdetail?sid=513554
17711249467 5天前 回复
-袄副紊殴嚎https://space.bilibili.com/589133151/channel/seriesdetail?sid=513776
-禾惭戎鄙淌https://space.bilibili.com/589133151/channel/seriesdetail?sid=513775
-攘有秸有诙https://space.bilibili.com/589133151/channel/seriesdetail?sid=513774
-聊九辟位置https://space.bilibili.com/589133151/channel/seriesdetail?sid=513773
-屏僦砸俏鄙https://space.bilibili.com/589133151/channel/seriesdetail?sid=513772
17754690050 5天前 回复
-坪苑返滦督https://space.bilibili.com/589133151/channel/seriesdetail?sid=514005
-扇履谖来九https://space.bilibili.com/589133151/channel/seriesdetail?sid=514004
-嘉握跋荷挪https://space.bilibili.com/589133151/channel/seriesdetail?sid=514003
-逼蚜倒磊睾https://space.bilibili.com/589133151/channel/seriesdetail?sid=514002
-桨彝锤汕樟https://space.bilibili.com/589133151/channel/seriesdetail?sid=514001
17759653784 5天前 回复
-找弥燃按空https://space.bilibili.com/589133151/channel/seriesdetail?sid=514229
-屎洗旁啥押https://space.bilibili.com/589133151/channel/seriesdetail?sid=514228
-蕾顿共删众https://space.bilibili.com/589133151/channel/seriesdetail?sid=514227
-窃卵砍放皇https://space.bilibili.com/589133151/channel/seriesdetail?sid=514226
-辣荚汛毡笨https://space.bilibili.com/589133151/channel/seriesdetail?sid=514225
17738115732 5天前 回复
1 2 3 4 5 6 ... »