execute method

  1. @override
int? execute(
  1. String sqlToExecute, {
  2. List? arguments,
  3. bool getLastInsertId = false,
  4. String? primaryKey,
})
override

Execute an insert, update or delete using sqlToExecute in normal or prepared mode using arguments.

This returns the number of affected rows. Only if getLastInsertId is set to true, the id of the last inserted row is returned. The primaryKey is sometimes necessary to retrieve the last inserted id.

Implementation

@override
int? execute(String sqlToExecute,
    {List<dynamic>? arguments,
    bool getLastInsertId = false,
    String? primaryKey}) {
  checkOpen();
  PreparedStatement? stmt;
  try {
    stmt = _db?.prepare(sqlToExecute);

    List<Object?> args = [];
    arguments?.forEach((element) {
      args.add(element);
    });
    stmt?.execute(args);

    if (getLastInsertId) {
      return _db?.lastInsertRowId;
      // return _db.getLastInsertId();
    } else {
      return _db?.getUpdatedRows();
    }
  } finally {
    stmt?.dispose();
    // stmt?.close();
  }
}