mariadb_stm...ecute_direct(3)

πŸ“¦
version 13.0.2 Β· 4 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, debian trixie, fedora 43, fedora 44, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ¦‹ mariadb_stmt_execute_direct πŸ¦‹

okay so this one is from the MariaDB Connector/C manual, and it's genuinely one of my faves because it's giving "two jobs, one function" energy πŸ’…


✨ NAME ✨

mariadb_stmt_execute_direct β€” prepares a prepared statement and executes it. Like, both. In one call. She's efficient, she's multitasking, she's the main character 🫢


🌸 SYNOPSIS 🌸

#include <mysql.h>

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

πŸ’– DESCRIPTION πŸ’–

So basically, this function prepares and executes a statement β€” one that you already allocated earlier with mysql_stmt_init(3). And bestie, if your statement has parameters, it uses the current values of those parameter variables. No extra steps, no drama. Slay ✨


πŸŽ€ PARAMETERS πŸŽ€

  • stmt β€” a statement handle, which you already got from mysql_stmt_init(3). Like, you gotta have that first, queen.
  • query β€” the SQL statement. the whole vibe.
  • length β€” length of the SQL statement. yes, she needs to know how long it is.

⭐ RETURN VALUE ⭐

Returns zero on success, non-zero on failure. That's it, that's the tea πŸ“


πŸ’€ NOTES πŸ’€

  • Since the number of parameters of the statement is unknown before execution, it is mandatory to set the number of parameters via the mysql_stmt_attr_set(3) function. like, mandatory mandatory. not optional. don't skip this one πŸ™ˆ
  • If the SQL statement is a zero-terminated string, you can also pass -1 as length. convenient? yes. iconic? also yes.
  • The statement handle is intended for one-time execution. Reusing the statement handle might lead to unexpected behavior. no cap, just make a new one, it's not that deep πŸ’…
bestie please do not recycle that statement handle. she said one-time. she meant one-time.

🌈 HISTORY 🌈

This function was added in Connector/C 3.0, and it requires MariaDB 10.2 or later versions. periodt πŸŽ€


πŸ‘‘ SEE ALSO πŸ‘‘


🧚 EXAMPLE 🧚

okay so here's the whole thing, verbatim, no edits, iykyk:

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", mysql_stmt_error(stmt));
  mysql_stmt_close(stmt);
  return 1;
}

see how she does the no-params one first, then sets the param count, then binds, then goes again? that's the whole flow. chef's kiss πŸ”₯


and that's the page, bestie πŸ’œ this was Version 3.4 of the MariaDB Connector/C manual, page mariadb_stm...ecute_direct(3) β€” go forth and execute directly, mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from