SQLite insert, update, delete transaction in Swift

import UIKit
import SQLite3

//Expense table
let TBL_EXPENSE = "Expenses"
let FLD_AUTO_EXPENSE_ID = "Exp_Id"
let FLD_TITLE = "Exp_Title"
let FLD_DATE = "Exp_Date"
let FLD_AMOUNT = "Exp_Amount"
let FLD_CATEGORY = "Exp_Category"
let FLD_PAID_STATUS = "Exp_Paid_Status"


class DatabaseView: NSObject {

    var fileManager: FileManager?
    var databasePath: String = ""
    private var database: OpaquePointer? = nil
    private var statement: OpaquePointer? = nil
    
    override init() {
        fileManager = FileManager.default
        var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let docsPath = paths[0]
        databasePath = URL(fileURLWithPath: docsPath).appendingPathComponent("Expenses").absoluteString
        print("DB Path : \(databasePath)")
    }
    
    func openDatabaseConnection() -> Bool {
        if sqlite3_open(databasePath, &database) == SQLITE_OK {
            return true
        }
        
        return false
    }
    
    func closeDatabaseConnection() {
        sqlite3_close(database)
    }

    func createDB() {
        if fileManager?.fileExists(atPath: databasePath) == false {
            createTable()
        } else {
            if fileManager?.fileExists(atPath: databasePath) == true {
                print("Database Already Exists.")
                createTable()
            } else {
                print("Error : \(String(describing: sqlite3_errmsg(database)))")
                print("Database Not Created / Opened.")
            }
        }
    }
    
    func createTable() {
        defer {
            closeDatabaseConnection()
        }
        do {
            if openDatabaseConnection() {
               // Expense table
                let CREATE_TABLE = "CREATE TABLE IF NOT EXISTS \(TBL_EXPENSE) (\(FLD_AUTO_EXPENSE_ID) INTEGER PRIMARY KEY AUTOINCREMENT, \(FLD_TITLE) TEXT, \(FLD_DATE) TEXT, \(FLD_AMOUNT) TEXT, \(FLD_CATEGORY) TEXT, \(FLD_PAID_STATUS) TEXT)"
                print(CREATE_TABLE)
                if sqlite3_exec(database, CREATE_TABLE, nil, nil, nil) != SQLITE_OK {
                    print("CREATE_TABLE Table Error : \(String(describing: sqlite3_errmsg(database)))")
                } else {
                    print("CREATE_TABLE Table Created")
                }
            }
        }
        closeDatabaseConnection()
    }
    
    func clearDatabase() {
        if fileManager?.fileExists(atPath: databasePath) == true {
            let error: Error? = nil
            try? fileManager?.removeItem(atPath: databasePath)
            assert(error == nil, "Assertion: SQLite file deletion shall never throw an error.")
            if error == nil {
                createDB()
            }
        }
    }

    func insertData(model: DatabaseModel) -> Bool {
        var result = false
        defer {
            closeDatabaseConnection()
        }
        do {
            if openDatabaseConnection() {
                var Data_stmt: String? = nil
                Data_stmt = "INSERT INTO \(TBL_EXPENSE) (\(FLD_TITLE),\(FLD_DATE),\(FLD_AMOUNT),\(FLD_CATEGORY),\(FLD_PAID_STATUS)) VALUES (\'\(model.exp_title)\',\'\(model.exp_date)\',\'\(model.exp_amount)\',\'\(model.exp_category)\',\'\(model.exp_paid_status)\')"
                print(Data_stmt as Any)
                sqlite3_prepare_v2(database, Data_stmt, -1, &statement, nil)
                if sqlite3_step(statement) == SQLITE_DONE {
                    print("insert sucessfully")
                    result = true
                } else {
                    print("insert not sucessfully")
                    result = false
                }
                sqlite3_reset(statement)
            }
        }
        closeDatabaseConnection()
        return result
    }

    
    func GetExpensesData(category: String) -> [DatabaseModel] {
        var arr = [DatabaseModel]()
        defer {
            closeDatabaseConnection()
        }
        do {
            if openDatabaseConnection() {
                let select_stmt = "SELECT * FROM \(TBL_EXPENSE) WHERE \(FLD_CATEGORY) = \'\(category)\'"
                print(select_stmt)
                if sqlite3_prepare_v2(database, select_stmt, -1, &statement, nil) == SQLITE_OK {
                    while sqlite3_step(statement) == SQLITE_ROW {
                        let model = DatabaseModel()
                        model.exp_Id = Int(sqlite3_column_int(statement, 0))
                        model.exp_title = String(cString: sqlite3_column_text(statement, 1)!)
                        model.exp_date = String(cString: sqlite3_column_text(statement, 2)!)
                        model.exp_amount = String(cString: sqlite3_column_text(statement, 3)!)
                        model.exp_category = String(cString: sqlite3_column_text(statement, 4)!)
                        model.exp_paid_status = String(cString: sqlite3_column_text(statement, 5)!)
                        arr.append(model)
                    }
                    sqlite3_reset(statement)
                }
                return arr
            }
            closeDatabaseConnection()
            return arr
        }
    }
    
    func UpdateData(model: DatabaseModel) -> Bool {
        var result = false
        defer {
            closeDatabaseConnection()
        }
        do {
            if openDatabaseConnection() {
                let Data_stmt = "UPDATE \(TBL_EXPENSE) SET \(FLD_TITLE) = \'\(model.exp_title)\', \(FLD_DATE) = \'\(model.exp_date)\', \(FLD_AMOUNT) = \'\(model.exp_amount)\', \(FLD_CATEGORY) = \'\(model.exp_category)\', \(FLD_PAID_STATUS) = \'\(model.exp_paid_status)\' WHERE \(FLD_AUTO_EXPENSE_ID) = \'\(String(model.exp_Id))\'"
                print(Data_stmt)
                sqlite3_prepare_v2(database, Data_stmt, -1, &statement, nil)

                if sqlite3_step(statement) == SQLITE_DONE {
                    print("Data Updated.")
                    result = true
                } else {
                    print("Data Not Updated. Error \(String(describing: sqlite3_errmsg(database)))")
                    result = false
                }
                sqlite3_reset(statement)
                return result
            }
            closeDatabaseConnection()
            return result
        }
    }
    
   
    func DeleteFromID(ID: String) -> Bool {
        var result = false
        defer {
            closeDatabaseConnection()
        }
        do {
            if openDatabaseConnection() {
                let Data_stmt = "DELETE FROM \(TBL_EXPENSE) WHERE \(FLD_AUTO_EXPENSE_ID) = \'\(ID)\'"
                print(Data_stmt)
                sqlite3_prepare_v2(database, Data_stmt, -1, &statement, nil)
                
                if sqlite3_step(statement) == SQLITE_DONE {
                    print("Data deleted.")
                    result = true
                } else {
                    print("Data Not deleted. Error \(String(describing: sqlite3_errmsg(database)))")
                    result = false
                }
                sqlite3_reset(statement)
                return result
            }
            closeDatabaseConnection()
            return result
        }
    }
}