请选择 进入手机版 | 继续访问电脑版

dart sqlite orm 框架jaguar_query_sqflite教程

[复制链接]
滚雪球少年 发表于 2021-1-2 19:42:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
  引言:
没想到这sqlite的语法和Mysql差别照旧挺多的,搞得只好先去看看Sqlite的语法再来写
一开始我使用的是sqflite来操纵sqlite数据库,原声的执行sql来操纵数据库
又有人提了,得用orm框架,好吧,然后推了这个jaguar_query_sqflite,刚按照官网的英文教程来操纵的,可行,因此写上这篇博客记录下来,也可以资助其他人。
项目基于flutter ,pub ,开发工具 Adroid Studio
框架地点:https://pub.dev/packages/jaguar_query_sqflite/example


  • 引入依赖
  async 、 jaguar的依赖必须,其他为我所使用的可省略。sqflite为原生sql操纵的依赖
  1. dependencies:  flutter:    sdk: flutter  sqflite: ^1.1.5  async: ^2.2.0  grpc: ^2.8.0  protobuf: ^1.0.1  jaguar_query_sqflite: ^2.2.11
复制代码


  • 执行pub get大概flutter pub get 拉取一下依赖包
  • 创建实体类
  1. import 'package:flutter_app/src/generated/im.pb.dart';import 'package:jaguar_query_sqflite/jaguar_query_sqflite.dart';import 'dart:async';class User {  int id;  String phone;  String icon;  String nickname;  String introduction;  User();  User.make(this.id,this.phone,this.icon,this.nickname,this.introduction);  @override  String toString() {    return 'Usehttps://pub.dev/packages/jaguar_query_sqflite/exampler{id: $id, phone: $phone, icon: $icon, nickname: $nickname, introduction: $introduction}';  }}class UserBean{  final IntField id=new IntField("id");  final StrField phone=new StrField("phone");  final StrField icon=new StrField("icon");  final StrField nickname=new StrField("nickname");  final StrField introduction=new StrField("introduction");  String get tableName=> "t_user";  /// The adapter  SqfliteAdapter _adapter;  UserBean(SqfliteAdapter adapter){    this._adapter=adapter;  }  Future createTable() async {dizhi    final st = new Create(tableName, ifNotExists: true)        .addAutoPrimaryInt('id')        .addStr('phone')        .addStr('icon')        .addStr('nickname')        .addStr('introduction');    await _adapter.createTable(st);  }  Future insert(UserInfo userInfo) async {    Insert inserter = new Insert(tableName);    inserter.set(id, userInfo.id);    inserter.set(phone, userInfo.phone);    inserter.set(icon, userInfo.icon);    inserter.set(nickname, userInfo.nickname);    inserter.set(introduction, userInfo.introduction);    return await _adapter.insert(inserter);  }  Future findAll()async{    Find finder=new Find(tableName);    List maps=await (await _adapter.find(finder)).toList();    List users=new List();    for(Map map in maps){      User user=User.make(map['id'], map['phone'], map['icon'], map['nickname'], map['introduction']);      users.add(user);    }    return users;  }}dizhivoid main()async{  SqfliteAdapter adapter=new SqfliteAdapter("test.db");  await adapter.connect();  final bean= UserBean(adapter);  await bean.createTable();  UserInfo userInfo=UserInfo()..id=1..phone="16612341234"..icon="默认图标"..nickname="humorchen"..introduction="我是humorchen";  await bean.insert(userInfo);  List users=await bean.findAll();  print('查询出的所有');  print(users);}
复制代码



  • 官方示范
  1. // Copyright (c) 2016, teja. All rights reserved. Use of this source code// is governed by a BSD-style license that can be found in the LICENSE file.import 'dart:async';import 'dart:io';import 'package:jaguar_query_sqflite/jaguar_query_sqflite.dart';import 'package:sqflite/sqflite.dart';// The modelclass Post {  Post();  Post.make(this.id, this.msg, this.author);  int id;  String msg;  String author;  String toString() => '$id $msg $author';}/// The adapterSqfliteAdapter _adapter;/// The beanclass PostBean {  /// Field DSL for id column  final IntField id = new IntField('_id');  /// Field DSL for msg column  final StrField msg = new StrField('msg');  /// Field DSL for author column  final StrField author = new StrField('author');  /// Table name for the model this bean manages  String get tableName => 'posts';  Future createTable() async {    final st = new Create(tableName, ifNotExists: true)        .addInt('_id', primary: true)        .addStr('msg', isNullable: true)        .addStr('author', isNullable: true);    await _adapter.createTable(st);  }  /// Inserts a new post into table  Future insert(Post post) async {    Insert inserter = new Insert(tableName);    inserter.set(id, post.id);    inserter.set(msg, post.msg);    inserter.set(author, post.author);    return await _adapter.insert(inserter);  }  /// Updates a post  Future update(int id, String author) async {    Update updater = new Update(tableName);    updater.where(this.id.eq(id));    updater.set(this.author, author);    return await _adapter.update(updater);  }  /// Finds one post by [id]  Future findOne(int id) async {    Find updater = new Find(tableName);    updater.where(this.id.eq(id));    Map map = await _adapter.findOne(updater);    Post post = new Post();    post.id = map['_id'];    post.msg = map['msg'];    post.author = map['author'];    return post;  }  /// Finds all posts  Future findAll() async {    Find finder = new Find(tableName);    List maps = await (await _adapter.find(finder)).toList();    List posts = new List();    for (Map map in maps) {      Post post = new Post();      post.id = map['_id'];      post.msg = map['msg'];      post.author = map['author'];      posts.add(post);    }    return posts;  }  /// Deletes a post by [id]  Future remove(int id) async {    Remove deleter = new Remove(tableName);    deleter.where(this.id.eq(id));    return await _adapter.remove(deleter);  }  /// Deletes all posts  Future removeAll() async {    Remove deleter = new Remove(tableName);    return await _adapter.remove(deleter);  }}main() async {  _adapter = new SqfliteAdapter(await getDatabasesPath());  // Connect  await _adapter.connect();  final bean = new PostBean();  await bean.createTable();  // Delete all  await bean.removeAll();  // Insert some posts  await bean.insert(new Post.make(1, 'Whatever 1', 'mark'));  await bean.insert(new Post.make(2, 'Whatever 2', 'bob'));  // Find one post  Post post = await bean.findOne(1);  print(post);  // Find all posts  List posts = await bean.findAll();  print(posts);  // Update a post  await bean.update(1, 'rowling');  // Check that the post is updated  post = await bean.findOne(1);  print(post);  // Delete some posts  await bean.remove(1);  await bean.remove(2);  // Find a post when none exists  post = await bean.findOne(1);  print(post);  // Close connection  await _adapter.close();  exit(0);}
复制代码
来源:https://blog.csdn.net/HumorChen99/article/details/112044514
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


专注素材教程免费分享
全国免费热线电话

18768367769

周一至周日9:00-23:00

反馈建议

27428564@qq.com 在线QQ咨询

扫描二维码关注我们

Powered by Discuz! X3.4© 2001-2013 Comsenz Inc.( 蜀ICP备2021001884号-1 )