当前位置:   article > 正文

Flutter SqlLite数据库快速入门_com.tekartik.sqflite

com.tekartik.sqflite

 

一、前言

光阴荏苒,转眼间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).创建数据库和数据库表:

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

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

 

创建数据库、数据库表:

  1. await openDatabase(
  2.         path,
  3.         version:vers,
  4.         onUpgrade: (Database db, int oldVersion, int newVersion) async{
  5.           //数据库升级,只回调一次
  6.           print("数据库需要升级!旧版:$oldVersion,新版:$newVersion");
  7.         },
  8.         onCreate: (Database db, int vers) async{
  9.           //创建表,只回调一次
  10.           await db.execute(dbTables);
  11.           await db.close();
  12.         }
  13.     );

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

 

(3).增:

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

  1.  /// Execute a raw SQL INSERT query
  2.   ///
  3.   /// Returns the last inserted record id
  4.   Future<intrawInsert(String sql, [List<dynamic> arguments]);
  5.   Future<intinsert(String table, Map<String, dynamic> values,
  6.       {String nullColumnHack, ConflictAlgorithm conflictAlgorithm});

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

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

 

(4).删:

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

  1. Future<intrawDelete(String sql, [List<dynamic> arguments]);
  2.   /// Convenience method for deleting rows in the database.
  3.   ///
  4.   /// Delete from [table]
  5.   ///
  6.   /// [where] is the optional WHERE clause to apply when updating. Passing null
  7.   /// will update all rows.
  8.   ///
  9.   /// You may include ?s in the where clause, which will be replaced by the
  10.   /// values from [whereArgs]
  11.   ///
  12.   /// [conflictAlgorithm] (optional) specifies algorithm to use in case of a
  13.   /// conflict. See [ConflictResolver] docs for more details
  14.   ///
  15.   /// Returns the number of rows affected if a whereClause is passed in, 0
  16.   /// otherwise. To remove all rows and get a count pass "1" as the
  17.   /// whereClause.
  18.   Future<intdelete(String table, {String where, List<dynamic> whereArgs});

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

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

 

(5).改:

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

  1.   Future<intrawUpdate(String sql, [List<dynamic> arguments]);
  2.   /// Convenience method for updating rows in the database.
  3.   ///
  4.   /// Update [table] with [values], a map from column names to new column
  5.   /// values. null is a valid value that will be translated to NULL.
  6.   ///
  7.   /// [where] is the optional WHERE clause to apply when updating.
  8.   /// Passing null will update all rows.
  9.   ///
  10.   /// You may include ?s in the where clause, which will be replaced by the
  11.   /// values from [whereArgs]
  12.   ///
  13.   /// [conflictAlgorithm] (optional) specifies algorithm to use in case of a
  14.   /// conflict. See [ConflictResolver] docs for more details
  15.   Future<intupdate(String table, Map<String, dynamic> values,
  16.       {String where,
  17.       List<dynamic> whereArgs,
  18.       ConflictAlgorithm conflictAlgorithm});

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

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

 

(6).查:

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

  1.   Future<List<Map<String, dynamic>>> query(String table,
  2.       {bool distinct,
  3.       List<String> columns,
  4.       String where,
  5.       List<dynamic> whereArgs,
  6.       String groupBy,
  7.       String having,
  8.       String orderBy,
  9.       int limit,
  10.       int offset});
  11.   /// Execute a raw SQL SELECT query
  12.   ///
  13.   /// Returns a list of rows that were found
  14.   Future<List<Map<String, dynamic>>> rawQuery(String sql,
  15.       [List<dynamic> arguments]);

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

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

 

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

今天就学习到这里吧!

>>>支持我的话可以关注下我的公众号,一起学习Android、小程序、跨平台开发~

扫一扫关注我的微信公众号:程序猿在广东

本案例demo地址:

https://github.com/ChessLuo/flutter_data_srorage

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/483230
推荐阅读
相关标签
  

闽ICP备14008679号