swiftsqlitefmdb

How know if FMDB query succeeded


I want to get any answer (for know if my query it was success or failure) of the following query:

let rs = try self.database.executeQuery
    ("insert into Practice(old, number) values (?, ?)", 
     values: [old, number])

but only i get this

<FMResultSet: 0x60000289ab20>

while i try iterate the collection, never into inside the cycle while

while rs.next(){
    print("never")
}

which is wrong in my code, why I get a clean collection


Solution

  • You want to use executeUpdate when performing an query that updates your database, such as an INSERT, UPDATE, or DELETE SQL statement:

    let sql = "INSERT INTO practice(old, number) VALUES (?, ?)"
    try database.executeUpdate(sql, values: [old, number])
    

    Needless to say, the way you know whether INSERT succeeded or not is whether it throws an error.

    But executeQuery is only used if the SQL returns rows (e.g. a SELECT statement).


    In the case of UPDATE and DELETE, an error is thrown only if there was an error during the update. But (especially if you have a WHERE clause), you may also want to examine database.changes to see how many rows were affected.