mysql_library_init()
int mysql_library_init(int argc, char **argv, char
**groups)
Description
This function should be called to initialize the MySQL library
before you call any other MySQL function. If your application
uses the embedded server, this call starts the server and
initializes any subsystems (mysys,
InnoDB, and so forth) that the server uses.
In a non-multi-threaded environment, the call to
mysql_library_init() may be
omitted, because mysql_init()
will invoke it automatically as necessary. However,
mysql_library_init() is not
thread-safe in a multi-threaded environment, and thus neither is
mysql_init(), which calls
mysql_library_init(). You must
either call
mysql_library_init() prior to
spawning any threads, or else use a mutex to protect the call,
whether you invoke
mysql_library_init() or
indirectly via mysql_init().
This should be done prior to any other client library call.
After your application is done using the MySQL library, call
mysql_library_end() to clean
up. See Section 26.2.3.39, “mysql_library_end()”.
The argc and argv
arguments are analogous to the arguments to
main(). The first element of
argv is ignored (it typically contains the
program name). For convenience, argc may be
0 (zero) if there are no command-line
arguments for the server.
mysql_library_init() makes a
copy of the arguments so it is safe to destroy
argv or groups after the
call.
If you want to connect to an external server without starting
the embedded server, you have to specify a negative value for
argc.
The groups argument should be an array of
strings that indicate the groups in option files from which
options should be read. See Section 4.2.3.2, “Using Option Files”. The
final entry in the array should be NULL. For
convenience, if the groups argument itself is
NULL, the [server] and
[embedded] groups are used by default.
See Section 26.2.2, “C API Function Overview”, for additional usage information.
mysql_library_init() was added
in MySQL 5.0.3. For older versions of MySQL, call
mysql_server_init() instead.
Example
#include <mysql.h>
#include <stdlib.h>
static char *server_args[] = {
"this_program", /* this string is not used */
"--datadir=.",
"--key_buffer_size=32M"
};
static char *server_groups[] = {
"embedded",
"server",
"this_program_SERVER",
(char *)NULL
};
int main(void) {
if (mysql_library_init(sizeof(server_args) / sizeof(char *),
server_args, server_groups)) {
fprintf(stderr, "could not initialize MySQL library\n");
exit(1);
}
/* Use any MySQL API functions here */
mysql_library_end();
return EXIT_SUCCESS;
}
Return Values
Zero if successful. Non-zero if an error occurred.

User Comments
Add your own comment.