Thread: MySQL - first program - question about returned data

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

    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
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

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, 01: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, 04:40 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Extracting Data From Another Program
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 02-12-2002, 01:25 AM