Problem connecting to MySQL Database?

This is a discussion on Problem connecting to MySQL Database? within the C++ Programming forums, part of the General Programming Boards category; Ok, I almost finished my program, but there's one little problem. This is the code: request.h Code: // Create prototype ...

  1. #1
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39

    Problem connecting to MySQL Database?

    Ok, I almost finished my program, but there's one little problem.

    This is the code:

    request.h
    Code:
    // Create prototype from Request class
    class Request
    {
          public:
                 Request();
                 ~Request();
                 
                 // Main function
                 bool handle_requests();
                 
                 // MySQL Functions
                 bool connect_to_db();
                 
          protected:             
                 // Winamp vars
                 WINAMPCOMLib::IApplicationPtr winamp;
                 
                 // MySQL vars
                 MYSQL * mysql_conn;
                 MYSQL_RES * result;
                 MYSQL_ROW row;
                 
    };
    main.cpp
    Code:
    #include <windows.h>
    #include <mysql.h>
    #include <iostream>
    #include <stdio.h>
    
    #pragma comment(lib, "D:\\server\\mysql\\lib\\opt\\libmysql.lib")
    
    #import "c:\program files\winamp\plugins\gen_com.dll"
    #include "request.h"
    
    using namespace std;
    
    Request::Request()
    {
    	cout << "Create winamp instance" << endl;
    	// Create winamp instance
    	Request::winamp.CreateInstance(__uuidof(WINAMPCOMLib::Application));
    	
    	cout << "Done." << endl;			
    }
    Request::~Request()
    {
    	mysql_close(Request::mysql_conn);
    }
    
    bool Request::connect_to_db()
    {
    	cout << "Connecting to Database..." << endl;
    	mysql_init(Request::mysql_conn);
    	if(!mysql_real_connect(Request::mysql_conn, "localhost", "lucas", "***", "luckyradio", 0, NULL, 0))
    	{
    		cout << "Could not connect to database:\n" << mysql_error(Request::mysql_conn) << endl;
    		return false;
    	}
    	
    	cout << "Done." << endl;
    	return true;
    }
    
    bool Request::handle_requests()
    {
    	cout << "Started Winamp Request Handler" << endl;
    	while(true)
    	{
    		if(!Request::winamp->CurrentPos)
    		{
    			if(!mysql_query(Request::mysql_conn, "SELECT * FROM e107_wa_request ORDER BY request_id ASC LIMIT 1"))
    			{
    				cout << "Error in selecting requests:\n" << mysql_error(Request::mysql_conn) << endl;
    				return false;
    			}
    			
    			result = mysql_use_result(Request::mysql_conn);
    			row = mysql_fetch_row(result);
    			
    			long playlist_pos = reinterpret_cast<long>(row[1]);
    			
    			Request::winamp->put_PlayListPos(playlist_pos);
    			Request::winamp->Play();
    			
    			cout << "Requesting: " << Request::winamp->GetSongTitle(playlist_pos) << "..." << endl;  						
    		}
    	}
    	
    	return false;
    }
    
    int main()
    {
    	Request request;
    	if(request.connect_to_db())
    	{
    		request.handle_requests();
    	
    		cin.get();
    	}
    	
    	return 1;
    }
    I compile it with Visual C++ 2005 Express edition, and no errors appear. Compile succesfull

    But, when I try to start the program, I get this:

    http://www.aoe3capitol.nl/images/requester_error.png

    Does anyone know how I can fix this?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    I dont use this API (I use ADO & ADO.NET), but I think you are not allocating space for the connection struct...

    Try

    Code:
    MYSQL mysql_conn;
    
    ....
    
    
    mysql_init(&Request::mysql_conn);
    	if(!mysql_real_connect(&Request::mysql_conn, "localhost", "lucas", "***", "luckyradio", 0, NULL, 0))

  3. #3
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39
    Ah fixed it,

    It should be:

    Code:
    bool Request::connect_to_db()
    {
    	cout << "Connecting to Database..." << endl;
    	Request::mysql_conn = mysql_init(NULL);
    	if(!mysql_real_connect(Request::mysql_conn, "localhost", "lucas", "***", "luckyradio", 0, NULL, 0))
    	{
    		cout << "Could not connect to database:\n" << mysql_error(Request::mysql_conn) << endl;
    		return false;
    	}
    	
    	cout << "Done." << endl;
    	return true;
    }
    Last edited by MrLucky; 01-29-2006 at 12:09 PM.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Try dump the "Request::" as it's part of the same class it shouldnt be needed

    Also where you had "Request::mysql_conn" in your original code, it should be "&mysql_conn";

  5. #5
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39
    Fixed above problem, but I get now another error:

    http://www.aoe3capitol.nl/images/requester_error2.png

    Current Code:

    request.h
    Code:
    // Create prototype from Request class
    class Request
    {
          public:
                 Request();
                 ~Request();
                 
                 // Main function
                 bool handle_requests();
                 
                 // MySQL Functions
                 bool connect_to_db();
                 
          protected:             
                 // Winamp vars
                 WINAMPCOMLib::IApplicationPtr winamp;
                 
                 // MySQL vars
                 MYSQL * mysql_conn;
                 MYSQL_RES * result;
                 MYSQL_ROW row;
                 
    };
    main.cpp
    Code:
    #include <windows.h>
    #include <mysql.h>
    #include <iostream>
    #include <stdio.h>
    
    #pragma comment(lib, "D:\\server\\mysql\\lib\\opt\\libmysql.lib")
    
    #import "c:\program files\winamp\plugins\gen_com.dll"
    #include "request.h"
    
    using namespace std;
    
    Request::Request()
    {
    	cout << "Create winamp instance..." << endl;
    	// Create winamp instance
    	Request::winamp.CreateInstance(__uuidof(WINAMPCOMLib::Application));
    	
    	cout << "Done." << endl;			
    }
    Request::~Request()
    {
    	mysql_close(Request::mysql_conn);
    }
    
    bool Request::connect_to_db()
    {
    	cout << "Connecting to Database..." << endl;
    	Request::mysql_conn = mysql_init(NULL);
    	if(!mysql_real_connect(Request::mysql_conn, "localhost", "lucas", "***", "luckyradio", 0, NULL, 0))
    	{
    		cout << "Could not connect to database:\n" << mysql_error(Request::mysql_conn) << endl;
    		return false;
    	}
    	
    	cout << "Done." << endl;
    	return true;
    }
    
    bool Request::handle_requests()
    {
    	cout << "Winamp Request Handler started." << endl;
    	while(true)
    	{
    		if(!Request::winamp->CurrentPos)
    		{
    			if(!mysql_query(Request::mysql_conn, "SELECT * FROM e107_wa_request ORDER BY request_id ASC LIMIT 1"))
    			{
    				cout << "Error in selecting requests:\n" << mysql_error(Request::mysql_conn) << endl;
    				return false;
    			}
    			
    			result = mysql_use_result(Request::mysql_conn);
    			Request::row = mysql_fetch_row(result);
    			
    			long playlist_pos = reinterpret_cast<long>(row[1]);
    			
    			Request::winamp->put_PlayListPos(playlist_pos);
    			Request::winamp->Play();
    			
    			cout << "Requesting: " << Request::winamp->GetSongTitle(playlist_pos) << "..." << endl;  						
    		}
    	}
    	
    	return false;
    }
    
    int main()
    {
    	cout << "Starting winamp Request handler...." << endl;
    	
    	Request request;
    	if(request.connect_to_db())
    	{
    		request.handle_requests();
    	
    		cin.get();
    	}
    	
    	return 1;
    }

  6. #6
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39
    Nobody?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. linker problem with mysql
    By august_01 in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2005, 11:04 AM
  4. database problem
    By ashb in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2005, 03:26 PM
  5. Connecting to sql database on server
    By jverkoey in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21