executeUpdate method

Future<int> executeUpdate(
  1. String sql, {
  2. List? params,
  3. Map<String, dynamic>? namedParams,
})

Execute a DML statement (INSERT, UPDATE, DELETE) and return rows affected.

Use this method for data modification operations when you need to know how many rows were affected.

Parameters:

  • sql: The DML statement (INSERT, UPDATE, DELETE, MERGE)
  • params: Optional list of bind parameters (positional binding)

Returns: The number of rows affected by the operation.

Example - INSERT:

final rowsInserted = await connection.executeUpdate(
  'INSERT INTO employees (id, name, salary) VALUES (:1, :2, :3)',
  params: [101, 'John Doe', 50000],
);
print('Inserted $rowsInserted row(s)');

Example - UPDATE:

final rowsUpdated = await connection.executeUpdate(
  'UPDATE employees SET salary = salary * 1.1 WHERE dept_id = :1',
  params: [10],
);
print('Updated $rowsUpdated employees');

Example - DELETE:

final rowsDeleted = await connection.executeUpdate(
  'DELETE FROM employees WHERE hire_date < :1',
  params: [DateTime(2020, 1, 1)],
);
print('Deleted $rowsDeleted old records');

Important: This method does NOT automatically commit. You must call commit to make changes permanent:

final rows = await connection.executeUpdate('UPDATE ...');
await connection.commit();  // Commit the change

See also:

Implementation

Future<int> executeUpdate(String sql, {List<dynamic>? params, Map<String, dynamic>? namedParams}) async {
  _ensureConnected();

  final statement = await execute(sql, params: params, namedParams: namedParams);
  try {
    final rowsAffected = statement.rowsAffected;

    // Auto-commit if enabled
    if (_autocommit) {
      await commit();
    }

    return rowsAffected;
  } finally {
    await statement.close();
  }
}