一、前言
光陰荏苒,轉眼間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語句進行增刪改查操作:
今天就學習到這里吧!