At 11:00 AM -0600 12/11/07, Paul DuBois wrote:
>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.
Sorry.
I retract this statement, or wish to modify it. mysql_library_init()
before load_defaults() works okay for a client program.
The problem is that if you want to put mysql_library_init() before
load_defaults() in an embedded server program, you cannot pass arguments
from option files to the embedded server via the argc/argv arguments
to mysql_library_init().
Using MY_INIT(), load_defaults(), mysql_library_init() in that order
seems to be a sequence that will work for normal clients and embedded
applications. (I am thinking that it's a good idea to have a sequence
that allows you to use a given program in either way depending on
which library you like the program against: libmysqlclient vs libmysqld.)
>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
>
>--
>MySQL Code Commits Mailing List
>For list archives: http://lists.mysql.com/commits
>To unsubscribe: http://lists.mysql.com/commits?unsub=1
--
Paul DuBois, MySQL Documentation Team
Madison, Wisconsin, USA
MySQL AB, www.mysql.com