【问题标题】:MySQL C API: check if a field existsMySQL C API:检查字段是否存在
【发布时间】:2014-11-16 02:10:34
【问题描述】:

我是 c 上 mysql 编程的新手。我需要检查某个用户是否存在于表中。如何使用 C API 做到这一点? 这是我所拥有的,但它不起作用:

if (mysql_query(con, "SELECT * FROM Users WHERE UserName='user3'")) 
  {
      finish_with_error(con);
  }      
  MYSQL_RES *result = mysql_store_result(con);
  if (result == NULL) 
  {
      finish_with_error(con);
  }
  int count = mysql_field_count(con);
  printf("%d\n", count );

【问题讨论】:

  • 以什么方式失败?请附上相关的错误信息。
  • 检查MYSQL_FIELD类型,并使用mysql_fetch_field,然后你可以做类似MYSQL_FIELD *field = mysql_fetch_field(result); printf("%s\n", field->name);API data structures are listed here的操作
  • 我总是得到'1'的计数,使用当前代码。
  • 字段是指列。你想计算行数。尝试调用mysql_fetch_row - 如果你得到一行,那么用户就存在。

标签: mysql c linux


【解决方案1】:

对于任何有兴趣的人,这是我使用 cmets 编写的,最后最终使用了。

if (mysql_query(con, "SELECT field FROM table WHERE userName='user1'")) 
{
    finish_with_error(con);
}

MYSQL_RES *result = mysql_store_result(con);

if (result == NULL) 
{
    finish_with_error(con);
}  

MYSQL_ROW row;
MYSQL_FIELD *field;

if (row = mysql_fetch_row(result))
{
    printf("%s\n", row[0]);
    db_pwd = row[0];
}

【讨论】:

    猜你喜欢
    • 2017-10-26
    • 1970-01-01
    • 2015-12-04
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多