# mariadb\_stmt\_execute\_direct

## Syntax

```c
int mariadb_stmt_execute_direct(MYSQL_STMT * stmt, const char *query, size_t length);
```

* `stmt` - A statement handle, which was previously allocated by [mysql\_stmt\_init()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mysql_stmt_init.md).
* `query` SQL statement
* `length` Length of SQL statement

## Description

Prepares and executes a statement which was previously allocated by [mysql\_stmt\_init()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mysql_stmt_init.md), using the current values of the parameter variables if any parameters exist in the statement.

Returns zero on success, non-zero on failure.

This function was added in Connector/C 3.0 and requires [MariaDB 10.2](/docs/release-notes/community-server/old-releases/10.2/what-is-mariadb-102.md) or later versions.

{% hint style="info" %}

* Since the number of parameter of the statement is unknown before execution it is mandatory to set the number of parameters via the [mysql\_stmt\_attr\_set()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mysql_stmt_attr_set.md) function.
* If the SQL statement is a zero-terminated string, you can also pass -1 as length.
* The statement handle is intended for one-time execution. Reusing the statement handle might lead to unexpected behavior.
  {% endhint %}

## See Also

* [mysql\_stmt\_attr\_set()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mysql_stmt_attr_set.md)
* [mysql\_stmt\_bind\_param()](/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mysql_stmt_bind_param.md)

## Example

```c
static int execute_direct_example(MYSQL *mysql)
{
  MYSQL_STMT *stmt= mysql_stmt_init(mysql);
  MYSQL_BIND bind[2];
  int intval= 1;
  int param_count= 2;
  char *strval= "execute_direct_example";

  /* Direct execution without parameters */
  if (mariadb_stmt_execute_direct(stmt, "CREATE TABLE execute_direct (a int, b varchar(30))", -1))
    goto error;

  memset(&bind, 0, sizeof(MYSQL_BIND) * 2);
  bind[0].buffer_type= MYSQL_TYPE_SHORT;
  bind[0].buffer= &intval;
  bind[1].buffer_type= MYSQL_TYPE_STRING;
  bind[1].buffer= strval;
  bind[1].buffer_length= strlen(strval);

  /* set number of parameters */
  if (mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &param_count))
    goto error;

  /* bind parameters */
  if (mysql_stmt_bind_param(stmt, bind))
    goto error;

  if (mariadb_stmt_execute_direct(stmt, "INSERT INTO execute_direct VALUES (?,?)", -1))
    goto error;

  mysql_stmt_close(stmt);
  return 0;
error:
  printf("Error: %s\n", mysql_stmt_error(stmt));
  mysql_stmt_close(stmt);
  return 1;
}
```

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/connectors/mariadb-connector-c/api-prepared-statement-functions/mariadb_stmt_execute_direct.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
