Successfully migrate the application to SQLIte!
This commit is contained in:
+84
-28
@@ -1,45 +1,101 @@
|
||||
// https://docs.flutter.dev/cookbook/persistence/sqlite
|
||||
|
||||
// SQLite
|
||||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_expense_tracker/models/frequency.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Local
|
||||
import '/models/frequency.dart';
|
||||
import '/models/expense.dart';
|
||||
|
||||
void loadDB() async {
|
||||
// Avoid errors caused by flutter upgrade.
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
// Leaned on this example:
|
||||
// https://learnflutterwithme.com/sqlite
|
||||
class DatabaseHelper {
|
||||
DatabaseHelper._privateConstructor();
|
||||
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
|
||||
|
||||
final String frequencies =
|
||||
"'${Frequency.values.map((freq) => freq.title).join("','")}'";
|
||||
print(frequencies);
|
||||
static Database? _db;
|
||||
Future<Database> get db async => _db ??= await _initDatabase();
|
||||
|
||||
// Open the database and store the reference.
|
||||
final database = openDatabase(
|
||||
// Set the path to the database. Note: Using the `join` function from the
|
||||
// `path` package is best practice to ensure the path is correctly
|
||||
// constructed for each platform.
|
||||
join(await getDatabasesPath(), 'expense_tracker.db'),
|
||||
Future<Database> _initDatabase() async {
|
||||
Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
||||
String path = join(documentsDirectory.path, "com_hyperling_expense.db");
|
||||
return await openDatabase(
|
||||
path,
|
||||
version: 1,
|
||||
onCreate: _onCreate,
|
||||
);
|
||||
}
|
||||
|
||||
onCreate: (db, version) {
|
||||
// Run the CREATE TABLE statement on the database.
|
||||
return db.execute(
|
||||
"""
|
||||
Future _onCreate(Database db, int version) async {
|
||||
await db.execute("""
|
||||
CREATE TABLE expense
|
||||
( id INTEGER PRIMARY KEY
|
||||
, name TEXT
|
||||
, cost DOUBLE
|
||||
, frequency TEXT CHECK(frequency IN ($frequencies) )
|
||||
, name TEXT NOT NULL UNIQUE
|
||||
, cost DOUBLE NOT NULL
|
||||
, frequency TEXT NOT NULL
|
||||
, description TEXT
|
||||
)""",
|
||||
);
|
||||
},
|
||||
// Set the version. This executes the onCreate function and provides a
|
||||
// path to perform database upgrades and downgrades.
|
||||
version: 1,
|
||||
);
|
||||
)
|
||||
""");
|
||||
}
|
||||
|
||||
/// Expense Section
|
||||
///
|
||||
Future<List<Expense>> getExpenses() async {
|
||||
Database db = await instance.db;
|
||||
var expenses = await db.query("expense", orderBy: "name");
|
||||
List<Expense> expenseList = expenses.isNotEmpty
|
||||
? expenses.map((c) => Expense.fromMap(c)).toList()
|
||||
: [];
|
||||
return expenseList;
|
||||
}
|
||||
|
||||
Future<int> addExpense(Expense expense) async {
|
||||
Database db = await instance.db;
|
||||
return await db.insert(
|
||||
"expense",
|
||||
expense.toMap(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> removeExpense(int id) async {
|
||||
Database db = await instance.db;
|
||||
return await db.delete(
|
||||
"expense",
|
||||
where: "id = ?",
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> updateExpense(Expense expense) async {
|
||||
Database db = await instance.db;
|
||||
return await db.update(
|
||||
"expense",
|
||||
expense.toMap(),
|
||||
where: "id = ?",
|
||||
whereArgs: [expense.id],
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> checkExpenseNameExists(String name) async {
|
||||
Database db = await instance.db;
|
||||
var expenses = await db.query("expense",
|
||||
where: "name = ?", whereArgs: [name],);
|
||||
return expenses.isNotEmpty;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
/// Income Section
|
||||
///
|
||||
|
||||
///
|
||||
/// Liquid Asset Section
|
||||
|
||||
///
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user