一假瞬、前言
光陰荏苒义郑,轉眼間2019年快過去一大半了!Flutter也從年初的1.2正式版到現(xiàn)在的1.5正式版杈曲,并且4月底谷歌IO大會宣布Flutter支持WEB端開發(fā)嵌入式開發(fā)驰凛,至此F已經支持全端開發(fā)了,這足以看到谷歌對這框架的投入担扑;是時候花時間去學習與歸納Flutter知識了恰响!
二、sqflite插件
sqflite基于Sqlite開發(fā)的一款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).創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)庫表:
在創(chuàng)建數(shù)據(jù)庫前枢劝,需要設置創(chuàng)建數(shù)據(jù)庫的路徑:
//獲取數(shù)據(jù)庫路徑
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, dbName);
創(chuàng)建數(shù)據(jù)庫井联、數(shù)據(jù)庫表:
await openDatabase(
path,
version:vers,
onUpgrade: (Database db, int oldVersion, int newVersion) async{
//數(shù)據(jù)庫升級,只回調一次
print("數(shù)據(jù)庫需要升級!舊版:$oldVersion,新版:$newVersion");
},
onCreate: (Database db, int vers) async{
//創(chuàng)建表您旁,只回調一次
await db.execute(dbTables);
await db.close();
}
);
openDatabase方法是用來初始化數(shù)據(jù)庫的烙常,里面有數(shù)據(jù)庫的路徑、版本鹤盒,版本是用來區(qū)別新舊數(shù)據(jù)庫的蚕脏,如表的結構需要發(fā)生變化,則需要提高版本進行數(shù)據(jù)庫升級侦锯,這是將會回調onUpgrade方法蝗锥,我們需要在這方法里編寫版本升級的邏輯。onCreate方法是創(chuàng)建數(shù)據(jù)庫時回調的方法率触,只執(zhí)行一次,因此我們需要在這里創(chuàng)建數(shù)據(jù)庫表汇竭。
(3).增:
即插入數(shù)據(jù)操作葱蝗,查看官方源碼:
/// 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方法第一個參數(shù)為一條插入sql語句,可以使用?作為占位符细燎,通過第二個參數(shù)填充數(shù)據(jù)两曼。
insert方法第一個參數(shù)為操作的表名,第二個參數(shù)map中是想要添加的字段名和對應字段值玻驻。
(4).刪:
即刪除數(shù)據(jù)操作悼凑,查看官方源碼:
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方法第一個參數(shù)為一條刪除sql語句,可以使用?作為占位符璧瞬,通過第二個參數(shù)填充數(shù)據(jù)户辫。
delete方法第一個參數(shù)為操作的表名,后邊的可選參數(shù)依次表示WHERE子句(可使用?作為占位符)嗤锉、WHERE子句占位符參數(shù)值渔欢。
(5).改:
即更新數(shù)據(jù)操作,查看官方源碼:
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方法第一個參數(shù)為一條更新sql語句瘟忱,可以使用?作為占位符奥额,通過第二個參數(shù)填充數(shù)據(jù)。
update方法第一個參數(shù)為操作的表名访诱,第二個參數(shù)為修改的字段和對應值垫挨,后邊的可選參數(shù)依次表示WHERE子句(可使用?作為占位符)、WHERE子句占位符參數(shù)值触菜、發(fā)生沖突時的操作算法(包括回滾九榔、終止、忽略等等)。
(6).查:
即查詢數(shù)據(jù)操作帚屉,查看官方源碼:
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方法第一個參數(shù)為操作的表名谜诫,后面的可選參數(shù)依次表示是否去重、查詢字段攻旦、where子句(可使用?作為占位符)喻旷、where占位符參數(shù)、GROUP BY 牢屋、haveing且预、ORDER BY、查詢的條數(shù)烙无、查詢的偏移位锋谐;
rawQuery方法第一個參數(shù)為一條sql查詢語句,可使用?占位符截酷,通過第二個arg參數(shù)填充占位的數(shù)據(jù)涮拗。
學習了基礎后,我簡單寫了個demo迂苛,主要利用SQL語句進行增刪改查操作:
今天就學習到這里吧三热!