MySQL - first program - question about returned data

This is a discussion on MySQL - first program - question about returned data within the C Programming forums, part of the General Programming Boards category; I wrote my first MySQL program in C last night. Seems to work pretty good. However, for about 30 minutes, ...

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309

    MySQL - first program - question about returned data

    I wrote my first MySQL program in C last night. Seems to work pretty good.

    However, for about 30 minutes, I was stuck attempting to output the returned data. I have a simple table

    CREATE TABLE dbname.tbname (col1 INTEGER NOT NULL) ;

    I inserted 50 rows in a loop and then selected the data:

    SELECT * FROM dbname.tbname ORDER BY col1 ;

    Upon fetching the rows,

    Code:
    	// Get the handle for the result table 
    	
    	result_set = mysql_use_result(mysql) ; 
    
    	if (!result_set) { 
    		fprintf(stderr, "use_result failed.  Error: %d, %s\n", 
    			mysql_errno(mysql), mysql_error(mysql) ) ;  
    		return -1 ; 
    	} 
    	else { 
    		printf("Use Result worked!\n") ; 
    	} 
    	
    	numcols = mysql_num_fields(result_set) ; 
    	printf("There are %d columns in the result table.\n", numcols );
    
    	while  ( row = mysql_fetch_row(result_set) ) {
    		for ( i = 0 ; i < numcols ; i++) {
    			printf("Row %d value = %s\n", i+1, row[i] );
    		}
    	}
    I was expecting to get the data back in an integer field, but MySQL decided to convert the numeric data to strings! What's up with that? Is there an option, perhaps, to have it not do that?

    Thanks
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    AFAIK it will return data in the form of strings, you can always use sscanf() to parse the data as an integer.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yes, that is indeed the case. I found this PDF: http://www.kitebird.com/mysql-book/ch06-2ed.pdf, and document page 356 explains the MYSQL_ROW interface.

    It appears I can prepare the statement though, and then get the raw data. Cool. Thanks.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get data from mysql and use in a system.
    By smaakage in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 02:25 PM
  2. program design question
    By theroguechemist in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2004, 08:45 PM
  3. Replies: 2
    Last Post: 05-12-2003, 05:40 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM
  5. Extracting Data From Another Program
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 02-12-2002, 01:25 AM

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