mariadb_stm...ecute_direct(3)
π¦ 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 frommysql_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
-1aslength. 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, ¶m_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
- alpine 3.22: mariadb-doc 11.4.13-r0 Β· GPL-2.0-or-later
- alpine 3.23: mariadb-doc 11.4.13-r0 Β· GPL-2.0-or-later
- alpine 3.24: mariadb-doc 11.8.9-r0 Β· GPL-2.0-only
- arch rolling: mariadb 13.0.2-2 Β· GPL-2.0-only
- arch rolling: mariadb-lts 12.3.3-2 Β· GPL-2.0-only
- centos-stream 10: mariadb-connector-c-doc 3.4.4-2.el10 Β· LGPL-2.1-or-later
- debian trixie: libmariadb-dev 1:11.8.6-0+deb13u1 Β· GPL-2.0-only
- fedora 43: mariadb-connector-c-doc 3.4.5-2.fc43 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01
- fedora 43: mariadb-connector-c-doc 3.4.9-2.fc43 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01 AND LicenseRef-Fedora-Public-Domain
- fedora 44: mariadb-connector-c-doc 3.4.8-3.fc44 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01
- fedora 44: mariadb-connector-c-doc 3.4.9-2.fc44 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01 AND LicenseRef-Fedora-Public-Domain
- nixpkgs unstable: mariadb-server 11.8.8
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- ubuntu questing: libmariadb-dev 1:11.8.3-1build1
- ubuntu resolute: libmariadb-dev 1:11.8.6-5 Β· GPL-2.0-only
- ubuntu resolute: libmariadb-dev 1:11.8.6-5ubuntu0.1 Β· GPL-2.0-only
- ubuntu stonking: libmariadb-dev 1:11.8.9+ds-4ubuntu2 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms