Error sqlite FMDB in swift - Unknown error calling sqlite3_step (5: database is locked)

0

I have the following code:

func downloadImagen() {
    print("Download Imagen")
    getOrders() { result, error in
        if error != nil {
            print("ERROR: \(error!)")
        }
        else {
            let imagenFondoBbDd: String = GestorDB.getImagenFondo()

            if (imagenFondoBbDd != "" && imagenFondoBbDd == result) {
                self.opt1()
            }
            else {
                if result != "null" {
                    GestorDB.updateCampoImagenFondo(result!)
                }
                else {
                    self.opt1()
                }
            }
        }
        return
    }
}

func getOrders(completionHandler: @escaping (String?, Error?) -> ()) {
    makeCall(completionHandler: completionHandler)
}

func makeCall(completionHandler: @escaping (String?, Error?) -> ()) {
    Alamofire.request(urlString, method: .post, parameters: parametros).responseString { response in
        switch response.result {
            case .success(let value):
                completionHandler(value, nil)
            case .failure(let error):
                completionHandler(nil, error)
        }
    }
}

The problem is when you do this:

GestorDB.updateCampoImagenFondo(result!)

What gives me this error:

  

Unknown error calling sqlite3_step (5: database is locked) eu

     

DB Query: UPDATE configuration SET field_1 =?, updated_at =? WHERE id = 1

I think the problem occurs when used in alamofire. If I take it out of that function, it works properly for me. How can I make it work?

EDITED

class func getImagenFondo() -> String {
    let ubicacionDB = self.obtenerRutaDB()
    let database = FMDatabase(path: ubicacionDB as String)
    if !(database.open()) {
        return ""
    }
    let sentenciaSql = "SELECT campo_1 FROM configuracion WHERE id = 1"
    do {
        let resultado:FMResultSet = try database.executeQuery(sentenciaSql, values: nil)
        while (resultado.next()) {
            if let string = resultado.string(forColumn: "aaa") {
                if let dataFromString = string.data(using: String.Encoding.utf8, allowLossyConversion: false) {
                    return NSString(data: dataFromString, encoding: String.Encoding.utf8.rawValue)! as String
                }
                else {
                    return ""
                }
            }
            else {
                return ""
            }
        }
        return ""
    }
    catch {
        return ""
    }
}

class func updateCampoImagenFondo(_ result: String) -> Bool {
    let ubicacionDB = self.obtenerRutaDB()
    let database = FMDatabase(path: ubicacionDB as String)
    if !(database.open()) {
        return false
    }
    else {
        let sentenciaSql = "UPDATE configuracion SET campo_1 = ? WHERE id = 1"
        if !(database.executeUpdate(sentenciaSql, withArgumentsIn: [result])) {
            database.close()
            let tituloError = "Error en la bbdd"
            let descError = "Error: No se puede ejecutar la query \(sentenciaSql)"
            Funciones().addError(tituloError, descripcion: descError, zona: 0, hashSeguridad: "")
            return false
        }
        else {
            //print("UPDATE CORRECT")
            return true
        }
    }
}
    
asked by Alberto Mier 25.01.2018 в 10:00
source

1 answer

1

In the getImagenFondo() method, the result set is missing.

And a programming suggestion: make a single return at the end.

class func getImagenFondo() -> String {
    let ubicacionDB = self.obtenerRutaDB()
    let database = FMDatabase(path: ubicacionDB as String)
    if !(database.open()) {
        return ""
    }
    let sentenciaSql = "SELECT campo_1 FROM configuracion WHERE id = 1"
    var imagenFondo = ""
    var resultado: FMResultSet? = nil
    defer {
        resultado?.close()
    }
    do {
        resultado = try database.executeQuery(sentenciaSql, values: nil)
        while (resultado!.next()) {
            if let string = resultado!.string(forColumn: "aaa"), let dataFromString = string.data(using: String.Encoding.utf8, allowLossyConversion: false) {
                imagenFondo = NSString(data: dataFromString, encoding: String.Encoding.utf8.rawValue)! as String
            }
        }
    }
    catch {}

    return imagenFondo
}

Note : verify that the code compiles, because I wrote it from memory ...

Another suggestion ... The code to connect to the database is repeated in both methods. You should extract it to a function:

class func getDatabase() -> FMDatabase? {
    let ubicacionDB = self.obtenerRutaDB()
    let database = FMDatabase(path: ubicacionDB as String)
    return database.open() ? database : nil
}
    
answered by 25.01.2018 в 14:26