Running OpenSUSE 11.2 in Sun VirtualBox 3.2.8 r54453
Installed MySQL connectors for C, Linux - Generic version
Compiling with GCC, using Code Blocks
I have a small routine that I am trying to get to connect to a MySQL database on the internet (not local box), which it does when I specify the values of my const chars directly. This code works...
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return(0);
}
/* send SQL query */
if (mysql_query(conn, "SELECT * FROM testtable"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return(0);
}
res = mysql_use_result(conn);
/* output fields 1 and 2 of each row */
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s %s\n", row[1], row[2]);
/* Release memory used to store results and close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}
However, when I attempt to obtain the values for the server, user, password and database from a file I get an error.
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
I checked this folder for the mysql.sock file and it doesnt exist but because the previous code works I guess the error is not really telling me the real problem.
fstream myfile ("sql.config");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
//get the position of the = sign so we can split the setting and the value
pos = line.find("=");
if (pos!=string::npos)
{
stext = line.substr(pos + 1);
posc = line.find("server=");
if (posc!=string::npos) {
server = new char(stext.length()+1);
server = stext.c_str();
}
posc = line.find("user=");
if (posc!=string::npos) {
user = new char(stext.length()+1);
user = stext.c_str();
}
posc = line.find("password=");
if (posc!=string::npos) {
password = new char(stext.length()+1);
password = stext.c_str();
}
posc = line.find("database=");
if (posc!=string::npos) {
database = new char(stext.length()+1);
database = stext.c_str();
}
}
}
myfile.close();
cout << server << endl;
cout << user << endl;
cout << password << endl;
cout << database << endl;
}
else
{
cout << "Cannot find sql.config" << endl;
}
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return(0);
}
/* send SQL query */
if (mysql_query(conn, "SELECT * FROM testtable"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
return(0);
}
res = mysql_use_result(conn);
/* output fields 1 and 2 of each row */
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s %s\n", row[1], row[2]);
/* Release memory used to store results and close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}
The values echoed to the screen with the 4 cout commands shows the correct values but I think something is going wrong with my conversion to const char from the string object. I have scanned the forums for a few hours now and tried various things but I cannot seem to get this resolved. Any help would be greatly appreciated.
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
when this message comes to the screen, I always establish a link to a path like "/usr/local/mysqldata/mysql.sock", and usually it turns to work.
How about have a try?
Thanks for responding. that file mysql.sock doesnt seem to exist anywhere. I am not sure what you mean by establish a link to a path, is that in the compiler settings? In any case, the code works fine when I set the values directly to the chars when they are initialised.
Hi kbw,
Thanks also for responding. The values have to be const char as the function called "mysql_real_connect" requires them as const char. I tried the square brackets and still get the same error. :(