• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


26 Apr, 2025

Updated at 18 May, 2025

SQLITE3 not updating in swift despite returning no errors

I'm having a bit of a problem with an sqlite3 implementation in swift not updating a balance field.

The table is defined as follows:

class User {
    var id: Int
    var uid: String
    var balance: Double
    var password: String
    var address: String
    
    init(id: Int, uid: String, balance: Double, password: String, address: String) {
        self.id = id
        self.uid = uid
        self.balance = balance
        self.password = password
        self.address = address
    }
}

It is being created with no issues.

I am writing an initial record at creation time with the following code:

  func insertUser(id: Int, uid: String, balance: Double, password: String) -> Bool{
        let users = getAllUsers()
        
        // Check user email is exist in User table or not
        for user in users{
            if user.id == id {
                return false
            }
        }
        
        let insertStatementString = "INSERT INTO User (id, uid, balance, password, address) VALUES (?, ?, ?, ?, ?);"
        var insertStatement: OpaquePointer? = nil
        
        if sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK {
            sqlite3_bind_int(insertStatement, 1, Int32(id))
            sqlite3_bind_text(insertStatement, 2, (uid as NSString).utf8String, -1, nil)
            sqlite3_bind_double(insertStatement, 3, Double(balance))
            sqlite3_bind_text(insertStatement, 4, (password as NSString).utf8String, -1, nil)
            sqlite3_bind_text(insertStatement, 5, "", -1, nil) // assign empty value to address

            if sqlite3_step(insertStatement) == SQLITE_DONE {
                print("User is created successfully.")
                sqlite3_finalize(insertStatement)
                return true
            } else {
                print("Could not add.")
                return false
            }
        } else {
            print("INSERT statement is failed.")
            return false
        }
    }

Up to this point, everything is working as expected. all fields are set appropriately.

I try to update the balance with the following code:

/ Update Earnings on User table
    func updateEarnings(id: Int, balance: Double) -> Bool {
        let updateStatementString = "UPDATE User set balance=? where id=?;"
        var updateStatement: OpaquePointer? = nil
        if sqlite3_prepare_v2(db,updateStatementString, -1, &updateStatement, nil) == SQLITE_OK {
            sqlite3_bind_double(updateStatement, 2, Double(balance))

            if sqlite3_step(updateStatement) == SQLITE_DONE {
                print("Earnings Updated Successfully.")
                sqlite3_finalize(updateStatement)
                return true
            } else {
                print("Could not update.")
                return false
            }
        } else {
            print("UPDATE statement is failed.")
            return false
        }
    }

I have verified that it and balance both have the appropriate values. It returns true but balance is never updated with the value passed in.

Any suggestions would be appreciated.