If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > Store VARCHAR (from MySQL table) to a CHAR/STRING Variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-18-11, 10:17
shinsengumi shinsengumi is offline
Registered User
 
Join Date: Feb 2011
Posts: 3
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.
Reply With Quote
Reply

Tags
char, c_programming, mysql, varchar, windowsxp

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On