Function exec

Execute an SQL command or prepared statement, such as INSERT/UPDATE/CREATE/etc.

ulong exec (
  Connection conn,
  const(char[]) sql,
  std.variant.VariantN!(32L)[] args
);

ulong exec (
  Connection conn,
  ref UnsafePrepared prepared,
  std.variant.VariantN!(32L)[] args
);

ulong exec (
  Connection conn,
  ref UnsafePrepared prepared
);

ulong exec(T...) (
  Connection conn,
  ref Prepared prepared,
  T args
)
if (T.length > 0 && !is(T[0] == Variant[]) && !is(T[0] == MySQLVal[]));

ulong exec(T...) (
  Connection conn,
  const(char[]) sql,
  T args
) @safe
if (!is(T[0] == Variant[]));

This method is intended for commands such as which do not produce a result set (otherwise, use one of the query functions instead.) If the SQL command does produces a result set (such as SELECT), MYXResultRecieved will be thrown.

If args is supplied, the sql string will automatically be used as a prepared statement. Prepared statements are automatically cached by mysql-native, so there's no performance penalty for using this multiple times for the same statement instead of manually preparing a statement.

If args and prepared are both provided, args will be used, and any arguments that are already set in the prepared statement will automatically be replaced with args (note, just like calling mysql.prepared.Prepared.setArgs, this will also remove all mysql.prepared.ParameterSpecialization that may have been applied).

Only use the const(char[]) sql overload that doesn't take args when you are not going to be using the same command repeatedly and you are CERTAIN all the data you're sending is properly escaped. Otherwise, consider using overload that takes a Prepared.

If you need to use any mysql.prepared.ParameterSpecialization, use mysql.connection.prepare to manually create a mysql.prepared.Prepared, and set your parameter specializations using mysql.prepared.Prepared.setArg or mysql.prepared.Prepared.setArgs.

Type Mappings

See the MySQL/D Type Mappings tables

Parameters

NameDescription
conn An open mysql.connection.Connection to the database.
sql The SQL command to be run.
prepared The prepared statement to be run.

Returns

The number of rows affected.

Example

auto myInt = 7;
auto rowsAffected = myConnection.exec("INSERT INTO `myTable` (`a`) VALUES (?)", myInt);