Thread: mysql database to listbox

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    mysql database to listbox

    Hello, I'm trying to output my database to a listbox...

    Code:
    MYSQL	*conn;
    MYSQL_RES  *result;		
    MYSQL_ROW  row;			
    
    
    void Mysql_Server_Connect( HWND hWnd ) 
    {	
    // create list box: to display database.																			
    HWND hWnd_List1 = CreateWindow( TEXT("LISTBOX"), NULL, WS_CHILD|WS_VSCROLL|LBS_DISABLENOSCROLL|WS_VISIBLE|LBS_STANDARD, 360, 165, 290,290, hWnd, NULL, ghInstance, NULL);
    
    conn = mysql_init(NULL);
    
    mysql_real_connect(conn, "localhost", "root", "password", "mydatabase", 0, NULL, 0) ;
    	
    mysql_query(conn, "SELECT * FROM drivers");
    result = mysql_store_result(conn);	
    
    TCHAR Buffer [50];
    		 
    for (unsigned int i=0; i< mysql_num_rows(result); i++) 
       {
            while( row = mysql_fetch_row(result) )
            {	
    	for (unsigned int k=0; k< mysql_num_fields(result); k++)
    	{
    	//unsigned long * lengths = mysql_fetch_lengths(result);
    	//wsprintf (Buffer, TEXT ("%lu"), lengths[k]);
    	//(const char*)SendMessage(hWnd_List1,  LB_INSERTSTRING, 0,
                      (LPARAM)Buffer );
    
    	 wsprintf (Buffer, TEXT ("%s"), row[k]);
    	(const char*)SendMessage(hWnd_List1,  LB_INSERTSTRING, 0,  
                     (LPARAM)Buffer );
    	}
                }
          }
    		
    
    mysql_free_result(result);
    mysql_close(conn);  
    }
    '%s'' doesn't seem to work,
    what should I use here?

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Either sprintf()
    Or TEXT("%S") - NOTE CAPITAL

    It depends on whether you're using sprintf or wsprintf, and/or have UNICODE defined.
    printf Type Field Characters (CRT)
    wsprintf Function (Windows)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Talking

    Hi Salem,

    TEXT("%S") works!!!

    Thank you !

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I would be worried about buffer over-runs with the buffer of only 49 chars (unless I knew all the DB fields had been limited to less than 50 characters).

    [the sprintf() functions insert a null terminator at the end of the string, so require one more char in buffer space than the biggest DB field can hold (or use a 'safe' version like _snprintf() if you can).]
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to connect borland c++ with mysql database
    By hdhd in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2010, 03:34 PM
  2. [C#] MySQl database problems
    By Jelte in forum C# Programming
    Replies: 5
    Last Post: 09-26-2008, 03:11 AM
  3. Problem connecting to MySQL Database?
    By MrLucky in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2006, 11:30 AM
  4. About C++ and MySQL or oether free database
    By xxxrugby in forum C++ Programming
    Replies: 18
    Last Post: 12-30-2005, 06:28 AM
  5. Can i use the ASP to access MYSQL database??
    By Frankiepoon in forum Windows Programming
    Replies: 1
    Last Post: 10-15-2002, 02:37 AM