Thread: Mysql++ and C++ and Windows Programming

  1. #1
    Registered User juschillin's Avatar
    Join Date
    Sep 2002
    Posts
    20

    Mysql++ and C++ and Windows Programming

    I can connect to mysql with with "Connection con" if I'm writing a windows application. But if I try to make a windows console application I cannot use "Connection con" to connect to the mysql database.

    Anybody no why?

    here's my include:

    #include <windows.h>

    #include "resource.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    #include <mysql++>
    #include <mysql++.h>
    #include <conio.h>
    #include <string.h>


    here's the part I get an error on:

    Connection con("my_database");

    This is the only part I have as far as connecting for now. I just want to see if I can connect before I can continue. Am I missing something?

    The error I get is this:

    C:\MySource\sorcehelp\source\source\ctl_one\ctl_on e.cpp(134) : error C2360: initialization of 'con' is skipped by 'case' label
    C:\MySource\sorcehelp\source\source\ctl_one\ctl_on e.cpp(130) : see declaration of 'con'
    Error executing cl.exe.


    -Thanks

  2. #2
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Show the code surrounding that statement. Is there a switch/case statement anywhere around there?

  3. #3
    Registered User juschillin's Avatar
    Join Date
    Sep 2002
    Posts
    20
    #include <windows.h>

    #include "resource.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    #include <mysql++>
    #include <mysql++.h>
    #include <conio.h>
    #include <string.h>



    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    switch(Message)
    {
    case WM_INITDIALOG:
    // This is where we set up the dialog box, and initialise any default values

    SetDlgItemText(hwnd, IDC_TEXT, "This is a string");
    SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE);
    break;
    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case IDC_ADD:
    {
    // When somebody clicks the Add button, first we get the number of
    // they entered

    BOOL bSuccess;
    int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
    if(bSuccess)
    {
    // Then we get the string they entered
    // First we need to find out how long it is so that we can
    // allocate some memory

    int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
    if(len > 0)
    {
    // Now we allocate, and get the string into our buffer

    int i;
    char* buf;

    buf = (char*)GlobalAlloc(GPTR, len + 1);
    GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);

    // Now we add the string to the list box however many times
    // the user asked us to.

    for(i = 0;i < nTimes; i++)
    {
    int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);

    // Here we are associating the value nTimes with the item
    // just for the heck of it, we'll use it to display later.
    // Normally you would put some more useful data here, such
    // as a pointer.
    SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
    }

    // Dont' forget to free the memory!
    GlobalFree((HANDLE)buf);
    }
    else
    {
    MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
    }
    }
    else
    {
    MessageBox(hwnd, "Couldn't translate that number ", "Warning", MB_OK);
    }

    }
    break;
    case IDC_REMOVE:
    {
    // When the user clicks the Remove button, we first get the number
    // of selected items

    HWND hList = GetDlgItem(hwnd, IDC_LIST);
    int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
    if(count != LB_ERR)
    {
    if(count != 0)
    {
    // And then allocate room to store the list of selected items.

    int i;
    int *buf = (int*)GlobalAlloc(GPTR, sizeof(int) * count);
    SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf);

    // Now we loop through the list and remove each item that was
    // selected.

    // WARNING!!!
    // We loop backwards, because if we removed items
    // from top to bottom, it would change the indexes of the other
    // items!!!

    for(i = count - 1; i >= 0; i--)
    {
    SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
    }

    GlobalFree(buf);
    }
    else
    {
    MessageBox(hwnd, "No items selected.", "Warning", MB_OK);
    }
    }
    else
    {
    MessageBox(hwnd, "Error counting items ", "Warning", MB_OK);
    }
    }
    break;
    case IDC_CLEAR:
    SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
    break;
    case IDC_GET:
    SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)TEXT("Attempting To Connect To Database"));
    SetDlgItemText(hwnd, IDC_SHOWCOUNT, "Attempting To Connect To Database...");




    //This is the Part I need Help With


    Connection con("tripletadmin");




    //End here



    break;
    case IDC_LIST:
    switch(HIWORD(wParam))
    {
    case LBN_SELCHANGE:
    {
    // Get the number of items selected.

    HWND hList = GetDlgItem(hwnd, IDC_LIST);
    int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
    if(count != LB_ERR)
    {
    // We only want to continue if one and only one item is
    // selected.

    if(count == 1)
    {
    // Since we know ahead of time we're only getting one
    // index, there's no need to allocate an array.

    int index;
    int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index);
    if(err != LB_ERR)
    {
    // Get the data we associated with the item above
    // (the number of times it was added)

    int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);

    SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data, FALSE);
    }
    else
    {
    MessageBox(hwnd, "Error getting selected item ", "Warning", MB_OK);
    }
    }
    else
    {
    // No items selected, or more than one
    // Either way, we aren't going to process this.
    SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-");
    }
    }
    else
    {
    MessageBox(hwnd, "Error counting items ", "Warning", MB_OK);
    }
    }
    break;
    }
    break;
    }
    break;
    case WM_CLOSE:
    EndDialog(hwnd, 0);
    break;
    default:
    return FALSE;
    }
    return TRUE;
    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
    return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
    }

  4. #4
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Please repost the code but put the code tags around it to make it readable.

    [ code ] your code here [ /code ]

    Like that except without the spaces between the brackets and the text.

  5. #5
    Registered User juschillin's Avatar
    Join Date
    Sep 2002
    Posts
    20
    Code:
    #include <windows.h>
    
    #include "resource.h"
    #include <stdio.h>        
    #include <stdlib.h>
    #include <iostream>
    #include <iomanip>
    #include <mysql++>
    #include <mysql++.h>   
    #include <conio.h>     
    #include <string.h>   
    
    
    
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
    		case WM_INITDIALOG:
    			// This is where we set up the dialog box, and initialise any default values
    
    			SetDlgItemText(hwnd, IDC_TEXT, "This is a string");
    			SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE);
    		break;
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    				case IDC_ADD:
    				{
    					// When somebody clicks the Add button, first we get the number of
    					// they entered
    
    					BOOL bSuccess;
    					int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
    					if(bSuccess) 
    					{
    						// Then we get the string they entered
    						// First we need to find out how long it is so that we can
    						// allocate some memory
    
    						int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
    						if(len > 0)
    						{
    							// Now we allocate, and get the string into our buffer
    
    							int i;
    							char* buf;
    
    							buf = (char*)GlobalAlloc(GPTR, len + 1);
    							GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
    
    							// Now we add the string to the list box however many times
    							// the user asked us to.
    
    							for(i = 0;i < nTimes; i++)
    							{
    								int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
    
    								// Here we are associating the value nTimes with the item 
    								// just for the heck of it, we'll use it to display later.
    								// Normally you would put some more useful data here, such
    								// as a pointer.
    								SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
    							}
    
    							// Dont' forget to free the memory!
    							GlobalFree((HANDLE)buf);
    						}
    						else 
    						{
    							MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
    						}
    					}
    					else 
    					{
    						MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK);
    					}
    
    				}
    				break;
    				case IDC_REMOVE:
    				{
    					// When the user clicks the Remove button, we first get the number
    					// of selected items
    
    					HWND hList = GetDlgItem(hwnd, IDC_LIST);
    					int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
    					if(count != LB_ERR)
    					{
    						if(count != 0)
    						{
    							// And then allocate room to store the list of selected items.
    
    							int i;
    							int *buf = (int*)GlobalAlloc(GPTR, sizeof(int) * count);
    							SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf);
    							
    							// Now we loop through the list and remove each item that was
    							// selected.  
    
    							// WARNING!!!  
    							// We loop backwards, because if we removed items
    							// from top to bottom, it would change the indexes of the other
    							// items!!!
    
    							for(i = count - 1; i >= 0; i--)
    							{
    								SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
    							}
    
    							GlobalFree(buf);
    						}
    						else 
    						{
    							MessageBox(hwnd, "No items selected.", "Warning", MB_OK);
    						}
    					}
    					else
    					{
    						MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
    					}
    				}
    				break;
    				case IDC_CLEAR:
    					SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
    				break;
    				case IDC_GET:
    					SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)TEXT("Attempting To Connect To Database"));
    					SetDlgItemText(hwnd, IDC_SHOWCOUNT, "Attempting To Connect To Database...");
    					Connection con("tripletadmin");
    					
    					
    				break;
    				case IDC_LIST:
    					switch(HIWORD(wParam))
    					{
    						case LBN_SELCHANGE:
    						{
    							// Get the number of items selected.
    
    							HWND hList = GetDlgItem(hwnd, IDC_LIST);
    							int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
    							if(count != LB_ERR)
    							{
    								// We only want to continue if one and only one item is
    								// selected.
    
    								if(count == 1)
    								{
    									// Since we know ahead of time we're only getting one
    									// index, there's no need to allocate an array.
    
    									int index;
    									int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index);
    									if(err != LB_ERR)
    									{
    										// Get the data we associated with the item above
    										// (the number of times it was added)
    
    										int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);
    
    										SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data, FALSE);
    									}
    									else 
    									{
    										MessageBox(hwnd, "Error getting selected item :(", "Warning", MB_OK);
    									}
    								}
    								else 
    								{
    									// No items selected, or more than one
    									// Either way, we aren't going to process this.
    									SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-");
    								}
    							}
    							else
    							{
    								MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
    							}
    						}
    						break;
    					}
    				break;
    			}
    		break;
    		case WM_CLOSE:
    			EndDialog(hwnd, 0);
    		break;
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
    }
    !Carpe Diem!
    "Sieze the Day"

  6. #6
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47

    Nice avatar

    When I compile that code I get a warning that there is a switch() statement without any case: or default: labels. Do you also get this warning? I am having trouble finding where that is but I would expect that to be the problem.

  7. #7
    Registered User juschillin's Avatar
    Join Date
    Sep 2002
    Posts
    20
    One sec I'm trying to compile it.


    I'll be right with you.
    !Carpe Diem!
    "Sieze the Day"

  8. #8
    Registered User juschillin's Avatar
    Join Date
    Sep 2002
    Posts
    20
    Not that I can remember I just did something and messed it up. So I can't compile right now.

    Anything you come up with would be great.

    Or if you get it working that would be awesome.
    !Carpe Diem!
    "Sieze the Day"

Popular pages Recent additions subscribe to a feed