At 11:18 AM +0100 12/11/07, Sergei Golubchik wrote:
>Hi!
>
>On Dec 07, Paul DuBois wrote:
>>
>> Current example (from third edition):
>>
>> #include <my_global.h>
>> #include <my_sys.h>
>> #include <mysql.h>
>>
>> static const char *client_groups[] = { "client", NULL };
>>
>> int
>> main (int argc, char *argv[])
>> {
>> int i;
>>
>> printf ("Original argument vector:\n");
>> for (i = 0; i < argc; i++)
>> printf ("arg %d: %s\n", i, argv[i]);
>>
>> MY_INIT (argv[0]);
>> load_defaults ("my", client_groups, &argc, &argv);
>>
>> printf ("Modified argument vector:\n");
>> for (i = 0; i < argc; i++)
>> printf ("arg %d: %s\n", i, argv[i]);
>>
>> exit (0);
>> }
>>
>> The header files are different, and it uses MY_INIT() rather
>> than my_init(). I seem to be using the same example for the
>> (now-in-progress) fourth edition. Is the current code okay?
>
>try to use mysql_library_init() instead of MY_INIT().
>And if load_defaults() will be in mysql.h then you won't need my_sys.h
>and my_global.h
For a program that actually connects to the server (rather than just
demonstrate load_defaults(), I have the following. One problem
with putting mysql_library_init() in place of MY_INIT() (instead
of where it is, later on in the program), is that load_defaults()
doesn't work then.
int
main (int argc, char *argv[])
{
int opt_err;
MY_INIT (argv[0]);
load_defaults ("my", client_groups, &argc, &argv);
if ((opt_err = handle_options (&argc, &argv, my_opts, get_one_option)))
exit (opt_err);
/* solicit password if necessary */
if (ask_password)
opt_password = get_tty_password (NULL);
/* get database name if present on command line */
if (argc > 0)
{
opt_db_name = argv[0];
--argc; ++argv;
}
/* initialize client library */
if (mysql_library_init (0, NULL, NULL))
{
print_error (NULL, "mysql_library_init() failed");
exit (1);
}
/* initialize connection handler */
conn = mysql_init (NULL);
if (conn == NULL)
{
print_error (NULL, "mysql_init() failed (probably out of memory)");
exit (1);
}
/* connect to server */
if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
{
print_error (conn, "mysql_real_connect() failed");
mysql_close (conn);
exit (1);
}
/* ... issue statements and process results here ... */
/* disconnect from server, terminate client library */
mysql_close (conn);
mysql_library_end ();
}
--
Paul DuBois, MySQL Documentation Team
Madison, Wisconsin, USA
MySQL AB, www.mysql.com