trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016
trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016, to work, but having issues with getting basic part to run. db_conn is not zero, and db_conn->host results in Access to field 'host' results in a dereference of a null pointer.
Code:
CBankTransaction::CBankTransaction(const std::string HOST, const std::string USER,
const std::string PASSWORD,
const std::string DATABASE)
{
db_conn = mysql_init(NULL);
if(db_conn)
{
db_conn = mysql_real_connect(db_conn, HOST.c_str(), USER.c_str(),
PASSWORD.c_str(), DATABASE.c_str(), 0, NULL, 0);
std::cout << "1: if(!db_conn) \n";
}
else
{
std::cout << "1: else \n";
std::cout <<"db_conn->host = " << db_conn->host << '\n';
}
if(!db_conn)
{
std::cout << "2: if(!db_conn) \n";
std::cout <<"db_conn->host = " << db_conn->host << '\n';
}
else
{
std::cout << "2: else \n";
std::cout <<"db_conn->host = " << db_conn->host << '\n';
}
}
both:
Code:
db_conn = mysql_init(NULL);
if(!db_conn)
and
Code:
db_conn = mysql_init(NULL);
if(db_conn)
result in trying to access a null pointer.
is mysql server suppose to be started from outside of program?
is there suppose to be a folder made somewhere, before program is run?
something i am missing that was stated on https://www.codeguru.com/database/da...ming-with-c-c/ ?
Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016
I found "Errors In case of insufficient memory, NULL is returned. "
how to set memory, and why was it not included in example?
Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016
Quote:
Originally Posted by
micha_eleric
how to set memory, and why was it not included in example?
DId you try to ask the author of the article?
Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016
Quote:
Originally Posted by
VictorN
DId you try to ask the author of the article?
could not find how to ask author, which was the first thing i looked for.
Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016
Code:
db_conn = mysql_real_connect(db_conn, HOST.c_str(), USER.c_str(),
PASSWORD.c_str(), DATABASE.c_str(), 0, NULL, 0);
The problem here is you trash your db_conn if the connect fails.
You can't go forward, you can't go back, you can't find out what happened.
Use something like this.
Code:
if (!mysql_real_connect(db_conn, HOST.c_str(), USER.c_str(),
PASSWORD.c_str(), DATABASE.c_str(), 0, NULL, 0))
{
std::cerr << "Failed to connect to database: Error: " << mysql_error(db_conn) << '\n';
}
Then you can look up the error on the man page and figure out why it didn't work.
https://dev.mysql.com/doc/c-api/8.0/...l-connect.html