dart sqlite orm 框架jaguar_query_sqflite教程

引言:
没想到这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操作的依赖

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 拉取一下依赖包
  • 创建实体类
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<Null> 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<List<User>> findAll()async{
    Find finder=new Find(tableName);
    List<Map> maps=await (await _adapter.find(finder)).toList();
    List<User> users=new List<User>();
    for(Map map in maps){
      User user=User.make(map['id'], map['phone'], map['icon'], map['nickname'], map['introduction']);
      users.add(user);
    }
    return users;
  }


}
dizhi
void 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<User> users=await bean.findAll();
  print('查询出的所有');
  print(users);
}

在这里插入图片描述

  • 官方示范
// 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 model
class Post {
  Post();

  Post.make(this.id, this.msg, this.author);

  int id;

  String msg;

  String author;

  String toString() => '$id $msg $author';
}

/// The adapter
SqfliteAdapter _adapter;

/// The bean
class 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<Null> 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<int> 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<Post> 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<List<Post>> findAll() async {
    Find finder = new Find(tableName);

    List<Map> maps = await (await _adapter.find(finder)).toList();

    List<Post> posts = new List<Post>();

    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<int> remove(int id) async {
    Remove deleter = new Remove(tableName);

    deleter.where(this.id.eq(id));

    return await _adapter.remove(deleter);
  }

  /// Deletes all posts
  Future<int> 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<Post> 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);
}
posted @ 2020-12-31 18:32  HumorChen99  阅读(0)  评论(0编辑  收藏  举报  来源