CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2024
    Posts
    15

    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

  2. #2
    Join Date
    Jan 2024
    Posts
    15

    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?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,407

    Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016

    Quote Originally Posted by micha_eleric View Post
    how to set memory, and why was it not included in example?
    DId you try to ask the author of the article?
    Victor Nijegorodov

  4. #4
    Join Date
    Jan 2024
    Posts
    15

    Re: trying to get Database Programming with C/C++, by Manoj Debnath July 11, 2016

    Quote Originally Posted by VictorN View Post
    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.

  5. #5
    Join Date
    Nov 2018
    Posts
    126

    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
  •  





Click Here to Expand Forum to Full Width

Featured