如何使用 .onDelete 中的值删除 FMDB 数据
How to use values in .onDelete to delete FMDB data
我是 Swift / SwiftUI 新手,正在尝试将我内置的 FMDB table 集成到我的应用程序中。我已经能够通过直接调用 FMDB tables 来填充我的列表,现在我希望能够从列表中删除一个项目以及 FMDB table 中相应的基础数据。我似乎无法弄清楚如何获取基础行数据才能 运行 SQL 删除相应的 table 数据。在 TABLE 的 Class 中,我有:
// 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
这是 table 中的唯一行 ID,以便我可以调用删除 SQL?
感谢上面的评论,我能够解决我遇到的问题。我
将数据库调用放入本地数组:
var budgetedExpenses: [BudgetedExpense.BudgetedExpenseRecord] {
return DBManager.shared.selectBudgetedExpense(account_code: self.account_code, budget_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
}
}
我是 Swift / SwiftUI 新手,正在尝试将我内置的 FMDB table 集成到我的应用程序中。我已经能够通过直接调用 FMDB tables 来填充我的列表,现在我希望能够从列表中删除一个项目以及 FMDB table 中相应的基础数据。我似乎无法弄清楚如何获取基础行数据才能 运行 SQL 删除相应的 table 数据。在 TABLE 的 Class 中,我有:
// 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
这是 table 中的唯一行 ID,以便我可以调用删除 SQL?
感谢上面的评论,我能够解决我遇到的问题。我 将数据库调用放入本地数组:
var budgetedExpenses: [BudgetedExpense.BudgetedExpenseRecord] { return DBManager.shared.selectBudgetedExpense(account_code: self.account_code, budget_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
}
}