Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2011
    Posts
    3

    Unanswered: Store VARCHAR (from MySQL table) to a CHAR/STRING Variable

    Hi all. I'd like to ask for a way to store a MySQL table content of type VARCHAR to a CHAR variable in C. The MySQL table has three columns: ID (int), Name (varchar(22)), Salary (int). My C program should ask the user to input an ID number, and depending on the input value, the program should look up in the table whose Name it belongs to, then store that name on a variable of type CHAR. I tried doing it but an error that says "Incompatible variable types" is returned when I compile the program. The function of my program that handles this functionality looks like the one below:

    Code:
    struct data_t {
    	char epc[23];
    };
    
    struct data_t data;
    
    void command1(MYSQL *conn, int input) { //retrieves Name to which the input ID belongs
      int num_rows;
      MYSQL_RES *res;
      MYSQL_ROW row;
      
      char cmd2[1024] = "";
      snprintf(cmd2,sizeof(cmd2),"select Name from table8 where id = %d", input);
      if (mysql_query(conn, cmd2)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(1);
      }
           
      res = mysql_use_result(conn);     
      row = mysql_fetch_row(res);
      data.epc = row[0];
      
      mysql_free_result(res);
    }
    At the line "data.epc = row[0];" above is where the error occurs. Are there other steps that I have to do first so that I could store the VARCHAR result into a CHAR variable in C? Answers/suggestions will be very much appreciated. Thanks in advance!

    PS: The structure contains other variables other than char epc[22] but I didn't include them here since they're not relevant to the topic. And by the way, WindowsXP is my operating system, but I hope Linux users could also give me answers. The syntax are very much similar anyway.

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    I am not sure but should MYSQL_ROW row be declared as a pointer? In other words MYSQL_ROW *row;
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    Join Date
    Feb 2011
    Posts
    3
    I've used MYSQL_ROW row before in my previous programs and it worked well for me. However, I'm not really sure if I should make it a pointer if I were to retrieve a VARCHAR from a table. To the best of my knowledge, MYSQL_ROW row retrieves strings of characters from a table after a query that's why I didn't make it a pointer anymore. For example, if I have a query like:

    Code:
    char cmd2[1024] = "";
      snprintf(cmd2,sizeof(cmd2),"select Salary from table8 where id = %d", input);
           
      res = mysql_use_result(conn);     
      row = mysql_fetch_row(res);
    The variable row contains a string of characters as a result of the query, but since Salary is what I need, I have to convert row from being a string to being an integer. So I will add a line to the code:

    Code:
    salary = atoi((const char *) row[0]);
    printf("%d\n", salary); //check if salary is indeed converted to an integer
    The code above works well for me without making MYSQL_ROW row a pointer.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •