我正在尝试遍历MySql表中的行,并将某列中的数据与使用C的某些用户输入进行比较。目前我的代码如下所示:
MYSQL *cxn = mysql_init(NULL);
MYSQL_RES *result;
unsigned int num_fields;
unsigned int num_rows;
char *query_string;
MYSQL_ROW *row;
if (mysql_real_connect(cxn, <em>/*blah blah blah**/</em>) == NULL) {
printf("Please restart the service.");
}
result = mysql_store_result(cxn);
num_rows = mysql_num_rows(result);
num_fields = mysql_num_fields(result);
while (row = mysql_fetch_row(result)) {
for (i = 0; i < num_fields; i++) {
//Confusion occurring here
//if (field[i] @ currentRow == "User input")
// printf("Win");
}
}
答案 0 :(得分:0)
如果您只查找与输入匹配的字段,则需要使用输入字符串搜索数据库。换句话说,编写查询字符串,以便它只为您提供与用户输入匹配的结果。这比搜索用户输入的每个返回行要快得多。
NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
if ([firstResponder isKindOfClass:[NSText class]] && (NSTextField*)[(NSText *)firstResponder delegate] == self.centeredText) {
[self.centeredText becomeFirstResponder];
}
如果确实需要返回每一行,那么您希望代码读取类似
的内容SELECT * FROM table WHERE Cars=input
但是,如果您的用户输入是字符串,则需要使用while (row = mysql_fetch_row(result)) {
for (i = 0; i < num_fields; i++) {
//Confusion occurring here
//if (row[i] == "User input") You can't compare strings in C with ==
if(strcmp(row[i], user_input) == 0))
printf("Win");
}
}
进行比较。
strcmp