summaryrefslogtreecommitdiffstats
path: root/libmariadb/man/mysql_select_db.3
blob: 5a70797c984f115880f1e9809726fc32a0b84b5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
.\" Automatically generated by Pandoc 2.5
.\"
.TH "mysql_select_db" "3" "" "Version 3.3.1" "MariaDB Connector/C"
.hy
.SS Name
.PP
mysql_select_db \- selects a database as default
.SS Synopsis
.IP
.nf
\f[C]
#include <mysql.h>

int mysql_select_db(MYSQL * mysql,
                    const char * db);
\f[R]
.fi
.SS Description
.PP
Selects a database as default.
Returns zero on success, non\-zero on failure
.SS Parameters
.IP \[bu] 2
\f[C]mysql\f[R] is a connection identifier, which was previously
allocated by \f[B]mysql_init(3)\f[R] and connected by
\f[B]mysql_real_connect(3)\f[R].
.IP \[bu] 2
\f[C]db\f[R] \- the default database name
.SS Notes
.IP \[bu] 2
To retrieve the name of the default database either execute the SQL
command \f[C]SELECT DATABASE()\f[R] or retrieve the value via
\f[B]mariadb_get_infov(3)\f[R] API function.
.IP \[bu] 2
The default database can also be set by the db parameter in
\f[B]mysql_real_connect(3)\f[R].
.SS Examples
.SS SQL
.IP
.nf
\f[C]
# switch to default database test
USE test;
# check default database
SELECT DATABASE();
+\-\-\-\-\-\-\-\-\-\-\-\-+
| database() |
+\-\-\-\-\-\-\-\-\-\-\-\-+
| test       |
+\-\-\-\-\-\-\-\-\-\-\-\-+
\f[R]
.fi
.SS MariadDB Connector/C
.IP
.nf
\f[C]
static int set_default_db(MYSQL *mysql)
{
  int rc;
  char *default_db;

  /* change default database to test */
  rc= mysql_select_db(mysql, \[dq]test\[dq]);
  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(\[dq]test\[dq], default_db) != NULL)
  {
    printf(\[dq]Wrong default database\[rs]n\[dq]);
    return 1;
  }
  printf(\[dq]Default database: %s\[dq], default_db);
  return 0;
}
\f[R]
.fi
.SS See also
.PP
\f[B]mysql_real_connect(3)\f[R]