create a update and delete function for sqlite that uses the following parameters “(using db: opaquepointer?, with query: string)” in swift with the option for specific errors thrown for each function in swift

main.swift
import SQLite3

enum DatabaseError: Error {
    case connectionError
    case queryError
    case updateError
    case deleteError
}

func updateRecord(using db: OpaquePointer?, with query: String) throws {
    guard let db = db else {
        throw DatabaseError.connectionError
    }
    
    var statement: OpaquePointer?
    if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK {
        if sqlite3_step(statement) != SQLITE_DONE {
            throw DatabaseError.updateError
        }
    } else {
        throw DatabaseError.queryError
    }
    sqlite3_finalize(statement)
}

func deleteRecord(using db: OpaquePointer?, with query: String) throws {
    guard let db = db else {
        throw DatabaseError.connectionError
    }
    
    var statement: OpaquePointer?
    if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK {
        if sqlite3_step(statement) != SQLITE_DONE {
            throw DatabaseError.deleteError
        }
    } else {
        throw DatabaseError.queryError
    }
    sqlite3_finalize(statement)
}
1059 chars
41 lines

related categories

gistlibby LogSnag