Flutter : sqliteの利用
Flutter + Firebase でアプリ作成のためのパーツあつめ。
1.ここまでに確認したこと
2.今回の確認
sqliteの利用
sqfliteのページのチュートリアルを見ながらコードを記述し、動作確認
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class DbTest {
void firstDbTest() async {
// OSなど環境別のDBディレクトリパスを取得する
var dbPath = await getDatabasesPath();
// DBファイルパス
dbPath = join(dbPath, 'demo.db');
print("DB Path $dbPath");
var helper = MemoProvider();
Database db = await helper.open(dbPath);
print("DB: ${db.toString()}");
var list = await helper.getAll();
if (list.length < 10) {
var newMemo = Memo();
newMemo.memo = "Memo ${DateTime.now()}";
helper.insert(newMemo);
}
for(Memo memo in list) {
print("MEMO : ${memo.toMap().toString()}");
}
}
}
class MemoMeta {
static String tableName = "Memo";
static String columnId = "_id";
static String columnMemo = "memo";
static List<String> columns() {
return [columnId, columnMemo];
}
}
class Memo {
int id;
String memo;
Map<String, dynamic> toMap() {
var map = <String, dynamic> {
MemoMeta.columnMemo: memo
};
if (id != null) {
map[MemoMeta.columnId] = id;
}
return map;
}
Memo();
Memo.fromMap(Map<String, dynamic> map) {
id = map[MemoMeta.columnId];
memo = map[MemoMeta.columnMemo];
}
}
class MemoProvider {
Database db;
Future<Database> open(String path) async {
var ddl = """
create table ${MemoMeta.tableName} (
${MemoMeta.columnId} integer primary key autoincrement,
${MemoMeta.columnMemo} text not null )
""";
print("DDL: $ddl");
db = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute(ddl);
}
);
return db;
}
Future<Memo> insert(Memo memo) async {
memo.id = await db.insert(MemoMeta.tableName, memo.toMap());
return memo;
}
Future<List<Memo>> getAll() async {
List<Map> maps = await db.query(
MemoMeta.tableName,
columns : MemoMeta.columns());
var list = new List<Memo>();
for(Map map in maps) {
list.add(Memo.fromMap(map));
}
return list;
}
Future<Memo> findById(int id) async {
List<Map> maps = await db.query(MemoMeta.tableName,
columns: MemoMeta.columns(),
where: '${MemoMeta.columnId} = ?',
whereArgs: [id]);
if (maps.length > 0) {
return Memo.fromMap(maps.first);
}
return null;
}
Future<int> delete(int id) async {
return await db.delete(
MemoMeta.tableName, where: '${MemoMeta.columnId} = ?', whereArgs: [id]);
}
Future<int> update(Memo memo) async {
return await db.update(MemoMeta.tableName, memo.toMap(),
where: '${MemoMeta.columnId} = ?', whereArgs: [memo.id]);
}
Future close() async => db.close();
}
ログに出力された。
I/flutter ( 3116): DB Path /data/user/0/info.typea.favo_phrase_trial/databases/demo.db
I/flutter ( 3116): DDL: create table Memo (
I/flutter ( 3116): _id integer primary key autoincrement,
I/flutter ( 3116): memo text not null )
I/flutter ( 3116): DB: 2 /data/user/0/info.typea.favo_phrase_trial/databases/demo.db
V/AudioManager( 3116): playSoundEffect effectType: 0
V/AudioManager( 3116): querySoundEffectsEnabled...
I/flutter ( 3116): MEMO : {memo: Memo 2019-12-09 21:18:12.816597, _id: 1}
I/flutter ( 3116): MEMO : {memo: Memo 2019-12-09 21:18:17.761803, _id: 2}
