mysql_select_db(3)
π mysql_select_db β bestie, let's pick a default database π
okay so this is a page from the MariaDB Connector/C manual, and it's about mysql_select_db, the little function that goes "yeah this database? it's the default now, we live here." π β¨
πΈ Name πΈ
mysql_select_db β selects a database as default. that's it, that's the whole job, and she does it flawlessly. π
β¨ Synopsis β¨
#include <mysql.h>
int mysql_select_db(MYSQL * mysql,
const char * db);
π¦ Description π¦
Selects a database as default. Returns zero on success, non-zero on failure β so like, 0 is the slay outcome, anything else means something went sideways, no cap. π
π Parameters π
mysqlβ this is a connection identifier, which was previously allocated bymysql_init(3)and connected bymysql_real_connect(3). think of it as your lil connection bestie that has to already exist before you start asking her to switch databases. π«Άdbβ the default database name. literally just the name of the database you want as your new default. π
π Notes π
- To retrieve the name of the default database, either execute the SQL command
SELECT DATABASE()or retrieve the value via themariadb_get_infov(3)API function. two routes, same destination, pick your fighter. β - The default database can also be set by the
dbparameter inmysql_real_connect(3). so if you set it at connect time you're already sorted β this function is for changing it after. π₯
π Examples π
π SQL
# switch to default database test
USE test;
# check default database
SELECT DATABASE();
+------------+
| database() |
+------------+
| test |
+------------+
look at that gorgeous little table. the default database is test and everyone clapped. π
π MariaDB Connector/C
static int set_default_db(MYSQL *mysql)
{
int rc;
char *default_db;
/* change default database to test */
rc= mysql_select_db(mysql, "test");
if (rc)
return rc; /* Error */
/* get the default database */
rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_SCHEMA, &default_db);
if (rc)
return rc; /* Error */
if (strcmp("test", default_db) != NULL)
{
printf("Wrong default database\n");
return 1;
}
printf("Default database: %s", default_db);
return 0;
}
so the whole vibe here is: switch the default db, then verify it actually switched, because trust but verify bestie. π΅οΈβοΈβ¨
heads up: if the check fails, this printsWrong default databaseand returns1. if everything's cute, it printsDefault database: %sand returns0. π don't skip the error checks, fr fr.
π§ See also π§
and that's the page!! thanks for reading, you're doing amazing sweetie. π
this was Version 3.4 of the MariaDB Connector/C manual, page mysql_select_db(3) β mwah, go forth and select your databases. ππ¦πΈ
π¦ 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