1mysql_select_db(3) MariaDB Connector/C mysql_select_db(3)
2
3
4
5 Name
6 mysql_select_db - selects a database as default
7
8 Synopsis
9 #include <mysql.h>
10
11 int mysql_select_db(MYSQL * mysql,
12 const char * db);
13
14 Description
15 Selects a database as default. Returns zero on success, non-zero on
16 failure
17
18 Parameters
19 • mysql is a connection identifier, which was previously allocated by
20 mysql_init(3) and connected by mysql_real_connect(3).
21
22 • db - the default database name
23
24 Notes
25 • To retrieve the name of the default database either execute the SQL
26 command SELECT DATABASE() or retrieve the value via mariadb_get_in‐
27 fov(3) API function.
28
29 • The default database can also be set by the db parameter in mysql_re‐
30 al_connect(3).
31
32 Examples
33 SQL
34 # switch to default database test
35 USE test;
36 # check default database
37 SELECT DATABASE();
38 +------------+
39 | database() |
40 +------------+
41 | test |
42 +------------+
43
44 MariadDB Connector/C
45 static int set_default_db(MYSQL *mysql)
46 {
47 int rc;
48 char *default_db;
49
50 /* change default database to test */
51 rc= mysql_select_db(mysql, "test");
52 if (rc)
53 return rc; /* Error */
54
55 /* get the default database */
56 rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_SCHEMA, &default_db);
57 if (rc)
58 return rc; /* Error */
59
60 if (strcmp("test", default_db) != NULL)
61 {
62 printf("Wrong default database\n");
63 return 1;
64 }
65 printf("Default database: %s", default_db);
66 return 0;
67 }
68
69 See also
70 mysql_real_connect(3)
71
72
73
74Version 3.3.1 mysql_select_db(3)