-
Notifications
You must be signed in to change notification settings - Fork 256
mysql_select_db
Georg Richter edited this page Aug 15, 2018
·
6 revisions
mysql_select_db - selects a database as default
#include <mysql.h>
int mysql_select_db(MYSQL * mysql,
const char * db);
-
mysql
- a mysql handle, which was previously allocated by mysql_init or mysql_real_connect. -
db
- the default database name
Selects a database as default. Returns zero on success, non-zero on failure
-
mysql
is a connection identifier, which was previously allocated by mysql_init() and connected by mysql_real_connect(). -
db
- the default database name
- To retrieve the name of the default database either execute the SQL command
SELECT DATABASE()
or retrieve the value via mariadb_get_infov() API function. - The default database can also be set by the db parameter in mysql_real_connect().
# switch to default database test
USE test;
# check default database
SELECT DATABASE();
+------------+
| database() |
+------------+
| test |
+------------+
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;
}
## See also
[mysql_real_connect](mysql_real_connect)
MariaDB Connector/C Reference