Handling SQL "Errors"

we are using GRDB with a standard approach.
try db_queue.write { db in
try db_queue.execute(sql: sql, arguments args)

But in recent test cases we discovered that an Update to a record referenced by row_id can fail and no error is thrown.

so for example:
update address set address_1 = :address1 ....... WHERE id = :id:

If the ID passed in is wrong (for example we try to update record with id of 100 and there are only 10 records in the table) we do not see any errors thrown.

Our Catch block is a wide open } catch let description {

Can anyone point me in the right direction please ?



Hello @Paul-TM,

An UPDATE SQL query does not throw any error when it updates no row at all.

Updating 0, 1, or 100 rows are all normal and expectable behaviors of such a query, depending on the WHERE clause and the content of the database, and WHERE id = ... is no exception.

In the specific case of SQLite, you can ask how many rows were updated right after executing the UPDATE query. With GRDB, it gives:

try db.execute(sql: "UPDATE address ...")
if db.changesCount == 0 {
  // throw some error

Note that GRDB record types come with this feature built-in (see Persistence Methods for more information):

// Throws PersistenceError.recordNotFound when
// there exist no row for the given primary key.
print(address.id) // prints some id
try address.update(db)

Hi Gwendal. The db.changesCount will work well for us.

Many thanks


Terms of Service

Privacy Policy

Cookie Policy