Bodungus
Bodungus

Reputation: 175

FMDB update statement doesnt use my values

Hello I am trying to execute an update to an existing row in my database. I have been using question marks ? inside my sql statements and giving FMDB an array of values to replace the ?'s with. Here is an example INSERT statement using this logic:

let sqlStatement = "INSERT INTO Cards (Column1, Column2, Column3) VALUES (?, ?, ?)"

try! db.executeUpdate(sqlStatement, values: Data)

Now I want to do this for an update statement, I've got the statement working in SQLPro, but I can't seem to get it to work on 1 line. Here is my SQL query that I am using outside of swift / fmdb:

UPDATE MyTable 
SET ColumnId='5', Column2='Helloworld', Column3='data' 
WHERE ColumnId='5'

Now here is my statement in swift:

let sqlStatement = "UPDATE Cards SET ColumnId='?', Column2='?', Column3='?'"

try! db.executeUpdate(sqlStatement, values: Data)

When I run this statement, the update goes through but all data in the db gets set to ?. I don't want to put my data into the sql statement string directly (as the documentation tells you not to, avoid sql injection and that).

How can I fix this statement so that I can update the sql database on my app?

Upvotes: 0

Views: 720

Answers (1)

Shivam Tripathi
Shivam Tripathi

Reputation: 1493

Update Statement will be like this :

-(void)updateValue:(Model *)data
    {
        [instance.database open];
        BOOL isUpdated=[instance.database executeUpdate:@"UPDATE Cards SET ColumnId =? , Column2 =? , Column3 =? WHERE ColumnId =?",data.ColumnId,data.Column2,data.Column3,data.ColumnId];
        [instance.database close];
    }

Swift Version :

func updateValue(_ data: Model) {
    instance.database.open()
    let isUpdated: Bool = instance.database.executeUpdate("UPDATE Cards SET ColumnId =? , Column2 =? , Column3 =? WHERE ColumnId =?", data.columnId, data.column2, data.column3, data.columnId)
    instance.database.close()
}

Upvotes: 1

Related Questions