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 > Database Server Software > MySQL > 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:21
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
  #2 (permalink)  
Old 02-18-11, 11:52
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
I am not sure but should MYSQL_ROW row be declared as a pointer? In other words MYSQL_ROW *row;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 02-19-11, 08:47
shinsengumi shinsengumi is offline
Registered User
 
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.
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