-
January 24th, 2024, 04:17 PM
#1
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/ ?
Last edited by VictorN; January 25th, 2024 at 02:20 AM.
Reason: adding the code tags
-
January 24th, 2024, 06:02 PM
#2
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?
-
January 25th, 2024, 02:32 AM
#3
Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016
 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?
Victor Nijegorodov
-
January 25th, 2024, 04:47 PM
#4
Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016
 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.
-
January 26th, 2024, 01:27 AM
#5
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|