converting string.c_str to const char

Hi,

Complete noob here...

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...

#include <winsock.h>
#include <mysql.h>
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{

MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

const char *server = "www.somewebsite.com";
const char *user = "username";
const char *password = "password";
const char *database = "db_name";

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;

}

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.

This is the code that doesn't work:

#include <winsock.h>
#include <mysql.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int main()
{

MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

const char *server;
const char *user;
const char *password;
const char *database;

string line;
string stext;
size_t pos;
size_t posc;

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.

Thanks in advance.

Greg J
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?
server = new char(stext.length()+1); ???

Should be:
server = new char[stext.length()+1];

But why are you allocating char arrays anyway? Why not:
std::string server = stext;
Hi KenSoGCN,

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. :(

Regards

Greg J
Treat strings as strings, they're in the C++ standard library for a reason.

You can use then as:
1
2
3
4
5
6
7
8
9
10
11
12
    if (!mysql_real_connect(
                conn,
                server.c_str(),
                user.c_str(),
                password.c_str(),
                database.c_str(),
                0,
                NULL,
                0))
    {
        //...
    }
brilliant! that works.

Thank you. very much appreciated. I clearly have quite a bit of learning to do...

Regards

Greg J
Topic archived. No new replies allowed.