Store VARCHAR (from MySQL table) to a CHAR/STRING Variable
Posted by: Shinsen Gumi
Date: February 18, 2011 09:04AM

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:

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.

Options: ReplyQuote


Subject
Views
Written By
Posted
Store VARCHAR (from MySQL table) to a CHAR/STRING Variable
1152
February 18, 2011 09:04AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.