Flutter SqlLite数据库快速入门

简介: Flutter SqlLite数据库快速入门

一、前言

光阴荏苒,转眼间2019年快过去一大半了!Flutter也从年初的1.2正式版到现在的1.5正式版,并且4月底谷歌IO大会宣布Flutter支持WEB端开发嵌入式开发,至此F已经支持全端开发了,这足以看到谷歌对这框架的投入;是时候花时间去学习与归纳Flutter知识了!

二、sqflite插件

sqflite基于Sqlite开发的一款flutter插件,其支持iOS和Android,

GitHub地址:https://github.com/tekartik/sqflite

三、集成使用

(1).导入sqflite插件库:

在pubspec.yaml文件中添加sqflite,当前版本1.1.5:

sqflite: ^1.1.5

在dart类中引入:

import 'package:sqflite/sqflite.dart';

(2).创建数据库和数据库表:

在创建数据库前,需要设置创建数据库的路径:

//获取数据库路径
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, dbName);

创建数据库、数据库表:

await openDatabase(
        path,
        version:vers,
        onUpgrade: (Database db, int oldVersion, int newVersion) async{
          //数据库升级,只回调一次
          print("数据库需要升级!旧版:$oldVersion,新版:$newVersion");
        },
        onCreate: (Database db, int vers) async{
          //创建表,只回调一次
          await db.execute(dbTables);
          await db.close();

        }
    );

openDatabase方法是用来初始化数据库的,里面有数据库的路径、版本,版本是用来区别新旧数据库的,如表的结构需要发生变化,则需要提高版本进行数据库升级,这是将会回调onUpgrade方法,我们需要在这方法里编写版本升级的逻辑。onCreate方法是创建数据库时回调的方法,只执行一次,因此我们需要在这里创建数据库表。

(3).增:

即插入数据操作,查看官方源码:

 /// Execute a raw SQL INSERT query
  ///
  /// Returns the last inserted record id
  Future<int> rawInsert(String sql, [List<dynamic> arguments]);

  Future<int> insert(String table, Map<String, dynamic> values,
      {String nullColumnHack, ConflictAlgorithm conflictAlgorithm});

rawInsert方法第一个参数为一条插入sql语句,可以使用?作为占位符,通过第二个参数填充数据。

insert方法第一个参数为操作的表名,第二个参数map中是想要添加的字段名和对应字段值。

(4).删:

即删除数据操作,查看官方源码:

Future<int> rawDelete(String sql, [List<dynamic> arguments]);

  /// Convenience method for deleting rows in the database.
  ///
  /// Delete from [table]
  ///
  /// [where] is the optional WHERE clause to apply when updating. Passing null
  /// will update all rows.
  ///
  /// You may include ?s in the where clause, which will be replaced by the
  /// values from [whereArgs]
  ///
  /// [conflictAlgorithm] (optional) specifies algorithm to use in case of a
  /// conflict. See [ConflictResolver] docs for more details
  ///
  /// Returns the number of rows affected if a whereClause is passed in, 0
  /// otherwise. To remove all rows and get a count pass "1" as the
  /// whereClause.
  Future<int> delete(String table, {String where, List<dynamic> whereArgs});

rawDelete方法第一个参数为一条删除sql语句,可以使用?作为占位符,通过第二个参数填充数据。

delete方法第一个参数为操作的表名,后边的可选参数依次表示WHERE子句(可使用?作为占位符)、WHERE子句占位符参数值。

(5).改:

即更新数据操作,查看官方源码:

  Future<int> rawUpdate(String sql, [List<dynamic> arguments]);

  /// Convenience method for updating rows in the database.
  ///
  /// Update [table] with [values], a map from column names to new column
  /// values. null is a valid value that will be translated to NULL.
  ///
  /// [where] is the optional WHERE clause to apply when updating.
  /// Passing null will update all rows.
  ///
  /// You may include ?s in the where clause, which will be replaced by the
  /// values from [whereArgs]
  ///
  /// [conflictAlgorithm] (optional) specifies algorithm to use in case of a
  /// conflict. See [ConflictResolver] docs for more details
  Future<int> update(String table, Map<String, dynamic> values,
      {String where,
      List<dynamic> whereArgs,
      ConflictAlgorithm conflictAlgorithm});

rawUpdate方法第一个参数为一条更新sql语句,可以使用?作为占位符,通过第二个参数填充数据。

update方法第一个参数为操作的表名,第二个参数为修改的字段和对应值,后边的可选参数依次表示WHERE子句(可使用?作为占位符)、WHERE子句占位符参数值、发生冲突时的操作算法(包括回滚、终止、忽略等等)。

(6).查:

即查询数据操作,查看官方源码:

  Future<List<Map<String, dynamic>>> query(String table,
      {bool distinct,
      List<String> columns,
      String where,
      List<dynamic> whereArgs,
      String groupBy,
      String having,
      String orderBy,
      int limit,
      int offset});

  /// Execute a raw SQL SELECT query
  ///
  /// Returns a list of rows that were found
  Future<List<Map<String, dynamic>>> rawQuery(String sql,
      [List<dynamic> arguments]);

query方法第一个参数为操作的表名,后面的可选参数依次表示是否去重、查询字段、where子句(可使用?作为占位符)、where占位符参数、GROUP BY 、haveing、ORDER BY、查询的条数、查询的偏移位;

rawQuery方法第一个参数为一条sql查询语句,可使用?占位符,通过第二个arg参数填充占位的数据。

学习了基础后,我简单写了个demo,主要利用SQL语句进行增删改查操作:

image.png

今天就学习到这里吧!

本案例demo地址:
https://github.com/ChessLuo/flutter_data_srorage

相关文章
|
9月前
|
存储 设计模式 Cloud Native
C++QT SqlLite数据库简单使用
C++QT SqlLite数据库简单使用
|
10月前
|
存储 JSON 数据库
Flutter必备技能:轻松掌握本地存储与数据库优化技巧!
Flutter必备技能:轻松掌握本地存储与数据库优化技巧!
165 0
|
28天前
|
SQL NoSQL 数据库
Flutter Hive NoSql 数据库使用指南
本文将会写一个 Hive CURD 的例子,详细介绍 Hive 这个轻量级的 Flutter 离线数据库的使用方法,包括 Hive 在 Flutter 开发中的重要性、Hive 与 SQLite 的比较等,帮助开发者快速上手 Hive 数据库。
Flutter Hive NoSql 数据库使用指南
|
5天前
|
JSON NoSQL Redis
Redis 作为向量数据库快速入门指南
Redis 作为向量数据库快速入门指南
11 1
|
8天前
|
前端开发 API Android开发
Flutter Canvas绘制快速入门
Flutter Canvas绘制快速入门
22 2
|
1月前
|
SQL 关系型数据库 MySQL
|
2月前
|
Java 数据库连接 API
后端开发之用Mybatis简化JDBC的开发快速入门2024及数据库连接池技术和lombok工具详解
后端开发之用Mybatis简化JDBC的开发快速入门2024及数据库连接池技术和lombok工具详解
39 3
|
2月前
|
存储 Oracle 关系型数据库
Oracle数据库快速入门
Oracle数据库快速入门
36 0
|
11月前
|
SQL 安全 关系型数据库
云数据库 RDS SQL Server 快速入门(二)
云数据库 RDS SQL Server 快速入门(二)
130 0
|
3月前
|
SQL 监控 关系型数据库
TiDB 分布式数据库快速入门详解
这些示例展示了TiDB的一些基本操作。实际使用时,你可能需要根据具体的业务需求和环境进行调整和优化。
330 4