2 messages in com.mysql.lists.plusplusreading config options from ~/.my.cnf...
FromSent OnAttachments
joe shoemaker20 Dec 2007 07:02 
Jonathan Wakely20 Dec 2007 12:43 
Subject:reading config options from ~/.my.cnf file
From:joe shoemaker (joem@gmail.com)
Date:12/20/2007 07:02:19 AM
List:com.mysql.lists.plusplus

Hello, I need help with MySQL C API to read ~/.my.cnf config options. I have a problem. connect2.c doesn't use client login info provided in ~/.my.cnf file? What would cause it to use 'joe'@localhost?

when I tried to execute the code in linux shell :

[joe@localhost sql]$ ./connect2 mysql_real_connect() failed Error 1045 (28000): Access denied for user 'joe'@'localhost' (using password: NO)

I have compiled my C client code with mysql-devel 6.0.3-0 on Fedora 6.

I am using the template code from book MySQL by Paul BuBois, Chapter 6 Section: Processing Command-Line Arguments, Accessing Option File Contents.

Here is the html format of Chapter6 pdf code:
http://72.14.205.104/search?q=cache:MUoAU8qMoRUJ:www.kitebird.com/mysql-book/ch06-2ed.pdf+static+struct+my_option+my_opts%5B%5D&hl=en&ct=clnk&cd=3&

~/.my.cnf: [client] user=some1 password=some2 host=localhost database=some3

connect2.c code:

#include <string.h> #include <my_global.h> #include <my_sys.h> #include <mysql.h> #include <my_getopt.h>

static char *opt_host_name = NULL; /* server host (default=localhost) */ static char *opt_user_name = NULL; /* username (default=login name) */ static char *opt_password = NULL; /* password (default=none) */ static unsigned int opt_port_num = 0; /* port number (use built-in value) */ static char *opt_socket_name = NULL; /* socket name (use built-in value) */ static char *opt_db_name = NULL; /* database name (default=none) */ static unsigned int opt_flags = 0; /* connection flags (none) */

static int ask_password = 0; /* whether to solicit password */

static MYSQL *conn; /* pointer to connection handler */

static const char *client_groups[] = { "client", NULL };

static void print_error (MYSQL *conn, char *message) { fprintf (stderr, "%s\n", message); if (conn != NULL) { #if MYSQL_VERSION_ID >= 40101 fprintf (stderr, "Error %u (%s): %s\n", mysql_errno (conn), mysql_sqlstate(conn), mysql_error (conn)); #else fprintf (stderr, "Error %u: %s\n", mysql_errno (conn), mysql_error (conn)); #endif } }

void process_result_set (MYSQL *conn, MYSQL_RES *res_set) { MYSQL_ROW row; unsigned int i;

while ((row = mysql_fetch_row (res_set)) != NULL) { for (i = 0; i < mysql_num_fields (res_set); i++) { if (i > 0) fputc ('\t', stdout); printf ("%s", row[i] != NULL ? row[i] : "NULL"); } fputc ('\n', stdout); } if (mysql_errno (conn) != 0) print_error (conn, "mysql_fetch_row() failed"); else printf ("%lu rows returned\n", (unsigned long) mysql_num_rows (res_set)); }

void process_real_statement (MYSQL *conn, char *stmt_str, unsigned int len) { MYSQL_RES *res_set;

if (mysql_real_query (conn, stmt_str, len) != 0) /* the statement failed */ { print_error (conn, "Could not execute statement"); return; }

/* the statement succeeded; determine whether it returned data */ res_set = mysql_use_result (conn); if (res_set) /* a result set was returned */ { /* process rows and then free the result set */ process_result_set (conn, res_set); mysql_free_result (res_set); } else /* no result set was returned */ { /* * does the lack of a result set mean that the statement didn't * return one, or that it should have but an error occurred? */ if (mysql_errno (conn) == 0) { /* * statement generated no result set (it was not a SELECT, * SHOW, DESCRIBE, etc.); just report rows-affected value. */ printf ("%lu rows affected\n", (unsigned long) mysql_affected_rows (conn)); } else /* an error occurred */ { print_error (conn, "Could not retrieve result set"); } } }

int main (int argc, char *argv[]) { int opt_err;

MY_INIT (argv[0]); load_defaults ("my", client_groups, &argc, &argv);

/* 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); }

unsigned long len; char stmt_buf[1024];

strcpy(stmt_buf,"SELECT id, title FROM post LIMIT 5;");

len = strlen(stmt_buf);

process_real_statement (conn, stmt_buf, len);

/* disconnect from server */ mysql_close (conn); exit (0); }

thanks, Joe