Add DEBUG messages to get methods. Fix the income and asset tables not existing if db upgrade is not run.
This commit is contained in:
parent
7947d64b3b
commit
029ee8e9a8
85
lib/db.dart
85
lib/db.dart
@ -3,6 +3,7 @@
|
|||||||
// SQLite
|
// SQLite
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:sqflite/sqflite.dart';
|
import 'package:sqflite/sqflite.dart';
|
||||||
@ -34,6 +35,26 @@ class DatabaseHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future _onCreate(Database db, int version) async {
|
Future _onCreate(Database db, int version) async {
|
||||||
|
debugPrint(
|
||||||
|
"_onCreate(): version=$version",
|
||||||
|
);
|
||||||
|
_createExpense(db);
|
||||||
|
_createIncome(db);
|
||||||
|
_createAsset(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future _onUpgrade(Database db, int previousVersion, int newVersion) async {
|
||||||
|
debugPrint(
|
||||||
|
"_onUpgrade(): previousVersion=$previousVersion, newVersion=$newVersion",
|
||||||
|
);
|
||||||
|
// Added in DB version 2.
|
||||||
|
if (previousVersion < 2 && newVersion >= 2) {
|
||||||
|
_createIncome(db);
|
||||||
|
_createAsset(db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _createExpense(Database db) async {
|
||||||
await db.execute("""
|
await db.execute("""
|
||||||
CREATE TABLE expense
|
CREATE TABLE expense
|
||||||
( id INTEGER PRIMARY KEY
|
( id INTEGER PRIMARY KEY
|
||||||
@ -41,41 +62,45 @@ class DatabaseHelper {
|
|||||||
, cost DOUBLE NOT NULL
|
, cost DOUBLE NOT NULL
|
||||||
, frequency TEXT NOT NULL
|
, frequency TEXT NOT NULL
|
||||||
, description TEXT
|
, description TEXT
|
||||||
)
|
)""");
|
||||||
""");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future _onUpgrade(Database db, int previousVersion, int newVersion) async {
|
void _createIncome(Database db) async {
|
||||||
// Added in DB version 2.
|
await db.execute("""
|
||||||
if (previousVersion < 2 && newVersion >= 2) {
|
CREATE TABLE income
|
||||||
await db.execute("""
|
( id INTEGER PRIMARY KEY
|
||||||
CREATE TABLE income
|
, name TEXT NOT NULL UNIQUE
|
||||||
( id INTEGER PRIMARY KEY
|
, revenue DOUBLE NOT NULL
|
||||||
, name TEXT NOT NULL UNIQUE
|
, frequency TEXT NOT NULL
|
||||||
, revenue DOUBLE NOT NULL
|
, description TEXT
|
||||||
, frequency TEXT NOT NULL
|
)""");
|
||||||
, description TEXT
|
}
|
||||||
)
|
|
||||||
""");
|
void _createAsset(Database db) async {
|
||||||
await db.execute("""
|
await db.execute("""
|
||||||
CREATE TABLE asset
|
CREATE TABLE asset
|
||||||
( id INTEGER PRIMARY KEY
|
( id INTEGER PRIMARY KEY
|
||||||
, name TEXT NOT NULL UNIQUE
|
, name TEXT NOT NULL UNIQUE
|
||||||
, amount DOUBLE NOT NULL
|
, amount DOUBLE NOT NULL
|
||||||
, description TEXT
|
, description TEXT
|
||||||
)
|
)""");
|
||||||
""");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expense Section
|
/// Expense Section
|
||||||
///
|
///
|
||||||
Future<List<Expense>> getExpenses() async {
|
Future<List<Expense>> getExpenses() async {
|
||||||
|
debugPrint("getExpenses(): Accessing db.");
|
||||||
Database db = await instance.db;
|
Database db = await instance.db;
|
||||||
|
|
||||||
|
debugPrint("getExpenses(): Querying table.");
|
||||||
var expenses = await db.query("expense", orderBy: "name");
|
var expenses = await db.query("expense", orderBy: "name");
|
||||||
|
|
||||||
|
debugPrint("getExpenses(): Putting into object list.");
|
||||||
List<Expense> expenseList = expenses.isNotEmpty
|
List<Expense> expenseList = expenses.isNotEmpty
|
||||||
? expenses.map((c) => Expense.fromMap(c)).toList()
|
? expenses.map((c) => Expense.fromMap(c)).toList()
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
|
debugPrint("getExpenses(): Returning!");
|
||||||
return expenseList;
|
return expenseList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,11 +146,18 @@ class DatabaseHelper {
|
|||||||
/// Income Section
|
/// Income Section
|
||||||
///
|
///
|
||||||
Future<List<Income>> getIncomes() async {
|
Future<List<Income>> getIncomes() async {
|
||||||
|
debugPrint("getIncomes(): Accessing db.");
|
||||||
Database db = await instance.db;
|
Database db = await instance.db;
|
||||||
|
|
||||||
|
debugPrint("getIncomes(): Querying table.");
|
||||||
var incomes = await db.query("income", orderBy: "name");
|
var incomes = await db.query("income", orderBy: "name");
|
||||||
|
|
||||||
|
debugPrint("getIncomes(): Putting into object list.");
|
||||||
List<Income> incomeList = incomes.isNotEmpty
|
List<Income> incomeList = incomes.isNotEmpty
|
||||||
? incomes.map((c) => Income.fromMap(c)).toList()
|
? incomes.map((c) => Income.fromMap(c)).toList()
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
|
debugPrint("getIncomes(): Returning!");
|
||||||
return incomeList;
|
return incomeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,10 +201,17 @@ class DatabaseHelper {
|
|||||||
///
|
///
|
||||||
/// Liquid Asset Section
|
/// Liquid Asset Section
|
||||||
Future<List<Asset>> getAssets() async {
|
Future<List<Asset>> getAssets() async {
|
||||||
|
debugPrint("getAssets(): Accessing db.");
|
||||||
Database db = await instance.db;
|
Database db = await instance.db;
|
||||||
|
|
||||||
|
debugPrint("getAssets(): Querying table.");
|
||||||
var assets = await db.query("asset", orderBy: "name");
|
var assets = await db.query("asset", orderBy: "name");
|
||||||
|
|
||||||
|
debugPrint("getAssets(): Putting into object list.");
|
||||||
List<Asset> assetList =
|
List<Asset> assetList =
|
||||||
assets.isNotEmpty ? assets.map((c) => Asset.fromMap(c)).toList() : [];
|
assets.isNotEmpty ? assets.map((c) => Asset.fromMap(c)).toList() : [];
|
||||||
|
|
||||||
|
debugPrint("getAssets(): Returning!");
|
||||||
return assetList;
|
return assetList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user