Sqlite database locked

Hi guys, 🙏I have a problem with repeated saving to the database. If I do the first update it is OK, but after the second attempt the update fails and the console says "Failed to commit transaction: database is locked". Here is my function:

func updateMapCoordinates(radius: Int32) { // Otevření databáze, pokud není otevřená guard db != nil else { print("Database connection is nil") return }

    var statement: OpaquePointer? = nil
    let updateQuery = "UPDATE \(mapTable) SET radius = ? WHERE id = 1;"

    // Začátek transakce
    if sqlite3_exec(db, "BEGIN TRANSACTION", nil, nil, nil) != SQLITE_OK {
        let errorMessage = String(cString: sqlite3_errmsg(db))
        print("Failed to begin transaction: \(errorMessage)")
        return
    }
    
    if sqlite3_prepare_v2(db, updateQuery, -1, &statement, nil) == SQLITE_OK {
        sqlite3_bind_int(statement, 1, radius)
        print("uložím \(radius)")
        
        if sqlite3_step(statement) == SQLITE_DONE {
            print("Coordinates saved successfully")
        } else {
            let errorMessage = String(cString: sqlite3_errmsg(db))
            print("Failed to save coordinates: \(errorMessage)")
        }
    } else {
        let errorMessage = String(cString: sqlite3_errmsg(db))
        print("SAVE statement could not be prepared: \(errorMessage)")
    }

    // Finalizace statementu
    sqlite3_finalize(statement)

    // Ukončení transakce
    if sqlite3_exec(db, "COMMIT", nil, nil, nil) != SQLITE_OK {
        let errorMessage = String(cString: sqlite3_errmsg(db))
        print("Failed to commit transaction: \(errorMessage)")
        sqlite3_exec(db, "ROLLBACK", nil, nil, nil)
    }
}
Answered by wendys in 792819022

Resolved! Problem was in database initialization, journal mode and synchronous mode must be set outside of transactions. Happy coding😀

Resolved! Problem was in database initialization, journal mode and synchronous mode must be set outside of transactions. Happy coding😀

Sqlite database locked
 
 
Q