mysql_select_db(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

πŸ’– 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 by mysql_init(3) and connected by mysql_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 the mariadb_get_infov(3) API function. two routes, same destination, pick your fighter. ⭐
  • The default database can also be set by the db parameter in mysql_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 prints Wrong default database and returns 1. if everything's cute, it prints Default database: %s and returns 0. πŸ’€ 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