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);
}
本文来自博客园,作者:HumorChen99,转载请注明原文链接:https://www.cnblogs.com/HumorChen/p/18039671
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~