赞
踩
光阴荏苒,转眼间2019年快过去一大半了!Flutter也从年初的1.2正式版到现在的1.5正式版,并且4月底谷歌IO大会宣布Flutter支持WEB端开发嵌入式开发,至此F已经支持全端开发了,这足以看到谷歌对这框架的投入;是时候花时间去学习与归纳Flutter知识了!
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语句进行增删改查操作:
今天就学习到这里吧!
>>>支持我的话可以关注下我的公众号,一起学习Android、小程序、跨平台开发~
扫一扫关注我的微信公众号:程序猿在广东
本案例demo地址:
https://github.com/ChessLuo/flutter_data_srorage
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。