MariaDB社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5325|回复: 2
打印 上一主题 下一主题

关于c语言中mysql的select语句变量查询问题

[复制链接]
跳转到指定楼层
1#
发表于 2009-4-8 09:46:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
程序如下:
#include <stdlib.h>
#include <stdio.h>
#include "mysql/mysql.h"
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
char *userName="z";//!
void display_row();
int main(int argc, char *argv[]) {
   int res;
   mysql_init(&my_connection);  
   if (mysql_real_connect(&my_connection, "localhost", "root",
                                              "111111", "zl", 0, NULL, 0)) {
   printf("Connection success\n");
   
   res = mysql_query(&my_connection, "SELECT username,password FROM user where='userName'");//!??
   if (res) {
      printf("SELECT error: %s\n", mysql_error(&my_connection));
   } else {
      res_ptr = mysql_use_result(&my_connection);
      if (res_ptr) {
         while ((sqlrow = mysql_fetch_row(res_ptr))) {
            printf("Fetched data...\n");
     display_row();
         }
         if (mysql_errno(&my_connection)) {
            printf("Retrive error: %s\n", mysql_error(&my_connection));
         }
      }
      mysql_free_result(res_ptr);
   }
   mysql_close(&my_connection);
   } else {
      fprintf(stderr, "Connection failed\n");
      if (mysql_errno(&my_connection)) {
         fprintf(stderr, "Connection error %d: %s\n",
                  mysql_errno(&my_connection), mysql_error(&my_connection));
      }
   }
   return EXIT_SUCCESS;
}
void display_row() {
   unsigned int field_count;
   field_count = 0;
   while (field_count < mysql_field_count(&my_connection)) {
      printf("%s ", sqlrow[field_count]);
      field_count++;
   }
   printf("\n");
}

运行之后程序说明可以连接成功,但是却无关于z的用户名输出,请问大家这是为什么呢?应该如何修改?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 转播转播 分享分享 分享淘帖 顶 踩
2#
发表于 2009-4-9 17:09:00 | 只看该作者
关注...
3#
发表于 2012-6-29 13:31:55 | 只看该作者

应该是SQL语句不完整吧,遗漏了字段名:

   res = mysql_query(&my_connection, "SELECT username,password FROM user where='userName'");//!??

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|MariaDB社区 ( 京ICP备07012489号    |
业务联系: QQ:48474881; 邮箱: 48474881@qq.com; 电话:13911732319
声明:本站部分文章是网友转载,若未经作者同意或署名有误,请联系网站管理员。

GMT+8, 2024-9-20 01:01 , Processed in 0.072603 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表