如何使用.onDelete中的值删除FMDB数据

时间:2020-07-20 21:32:37

标签: swiftui fmdb

我是Swift / SwiftUI新手,正在尝试将我内置的FMDB表集成到我的应用程序中。我已经能够通过直接调用FMDB表来填充列表,现在希望能够从列表中删除项目以及FMDB表中的相应基础数据。我似乎无法弄清楚如何获取基础行数据以能够运行SQL删除相应的表数据。在表的类中,我有:

// Budgeted_expense record for FMDB
struct BudgetedExpenseRecord: Hashable {
    var expense_id: Int!
    var account_code: Int!
    var budget_code: Int!
    var expense_code: Int!
    var budget_year: Int!
    var budget_month: Int!
    var description: String!
    var category: String!
    var expense_budget: Double!
    var expense_spent: Double!
    var unexpected_expense: Double!
    var category_code: Int!
    var hidden: Bool!
}

对DBMANAGER的调用是:

// Select BudgetedExpense records for a given year / month
func selectBudgetedExpense(account_code: Int, budget_code: Int, year: Int, month: Int) -> [BudgetedExpense.BudgetedExpenseRecord]
{
    var budgeted_expense_results: [BudgetedExpense.BudgetedExpenseRecord]
    if openDatabase(){
        budgeted_expense_results = BudgetedExpense.shared.selectBudgetedExpense(database: database, query: BudgetedExpense.shared.selectBudgetedExpense(account_code: account_code, budget_code: budget_code, year: year, month: month))
        if checkTables {
            print("\nQUERY: BudgetedExpense.shared.selectBudgetedExpense(account_code: \(account_code), budget_code: \(budget_code), year: \(year), month: \(month)")
            print(BudgetedExpense.shared.selectBudgetedExpense(account_code: account_code, budget_code: budget_code, year: year, month: month))
        }
        database.close()
        return budgeted_expense_results
    }else {
        print("Database was not opened.  Couldn't execute the query.  Returning empty results")
        fatalError("Database was not opened.  Couldn't execute the query.  Returning empty results")
    }
}

在我的SwiftUI视图中,我可以如下填充列表:

                List {
                    ForEach (DBManager.shared.selectBudgetedExpense(account_code: self.account_code, budget_code: self.budget_code, year:  self.budget_year, month: self.budget_month), id: \.self) { record in
                        BudgetedExpenseEditRow(
                            expense_id: record.expense_id,
                            account_code: record.account_code,
                            budget_code: record.budget_code,
                            budget_year: record.budget_year,
                            budget_month: record.budget_month,
                            description: record.description,
                            category: record.category,
                            expense_budget: roundToPlaces(value: record.expense_budget, 
                               places: 3),
                            expense_spent: record.expense_spent,
                            unexpected_expense: record.unexpected_expense,
                            hidden: record.hidden
                        )
                    }
                    .onDelete(perform: self.delete)

                    
                }.navigationBarTitle("Budgeted Expenses")

我还无法弄清楚如何onDelete来获得expense_id,它是表中唯一的行ID,因此我可以调用删除SQL?

1 个答案:

答案 0 :(得分:0)

由于上面的评论,我得以解决自己遇到的问题。一世 将数据库调用放入本地数组:

var budgetedExpenses:[BudgetedExpense.BudgetedExpenseRecord] { 返回DBManager.shared.selectBudgetedExpense(帐户代码:self.account_code,预算代码:self.budget_code,年份:self.budget_year,月份:self.budget_month)

}

然后我使用该数组填充视图。

                List {
                    ForEach (self.budgetedExpenses, id: \.self) { record in
                        BudgetedExpenseRow(
                            expense_id: record.expense_id,
                            account_code: record.account_code,
                            budget_code: record.budget_code,
                            expense_code: record.expense_code,
                            budget_year: record.budget_year,
                            budget_month: record.budget_month,
                            description: record.description,
                            category: record.category,
                            expense_budget: roundToPlaces(value: record.expense_budget, places: 3),
                            expense_spent: record.expense_spent,
                            unexpected_expense: record.unexpected_expense,
                            hidden: record.hidden
                        )
                    }
                    
                }.navigationBarTitle("Budgeted Expenses")

在onDelete中,我可以获取所需的数据。

func delete(at offsets: IndexSet) {
    offsets.forEach { index in
      let budgetExpense = self.budgetedExpenses[index]
        
        let queryString = ExpenseTransactions.shared.deleteExpenseTransactionsForYearQuery(acctcode: budgetExpense.account_code!, budgetcode: budgetExpense.budget_code!, expensecode: budgetExpense.expense_code!, year: budgetExpense.budget_year!) + "\n" +
            BudgetedExpenseDetails.shared.deleteBudgetedExpenseDetailsForYearQuery(acctcode: budgetExpense.account_code!, budgetcode: budgetExpense.budget_code!, expensecode: budgetExpense.expense_code!, year: budgetExpense.budget_year!) + "\n" +
            BudgetedExpense.shared.deleteBudgetedExpenseForYearQuery(acctcode: budgetExpense.account_code!, budgetcode: budgetExpense.budget_code!, expensecode: budgetExpense.expense_code!, year: budgetExpense.budget_year!) + "\n" +
            DBManagerSupport.shared.rollupBudgetTotals()
        
        if !DBManager.shared.executeSQLStatements(query: queryString)
        {
            print("Failed to delete table data")
        }
        var budget_result: Budget.BudgetRecord
           budget_result = DBManager.shared.selectBudgetYearMonth(acctCode: self.account_code, budgetCode: self.budget_code, budgetYear: self.budget_year, budgetMonth: self.budget_month)
                   self.total_budgeted_expense = budget_result.expected_expense
                   self.total_expense_spent = budget_result.expected_expense_spent
                   self.total_unexpected_expense = budget_result.unexpected_expense
        self.recordDeleted = true
        self.isEditing = false
    }
}