zoukankan      html  css  js  c++  java
  • MySQL C API 访问 MySQL 示例

    代码: 

    复制代码
    /* Simple C program that connects to MySQL Database server */ 
    #include <mysql.h> 
    #include <stdio.h>
    main() {   char *begin=" +--------------BEGIN---------------+ ";   printf(begin);
      MYSQL *conn;   MYSQL_RES *res;   MYSQL_ROW row;   char *server = "localhost";   char *user = "your mysql user";   char *password = "your password";   char *database = "your database";
      conn = mysql_init(NULL); /* Connect to database */
      /*    * CLIENT_MULTI_RESULTS    * 通知服务器,客户端能够处理来自多语句执行或存储程序的多个结果集。    * 如果设置了CLIENT_MULTI_STATEMENTS,将自动设置它。   */   if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, CLIENT_MULTI_RESULTS)) {     fprintf(stderr, "%s ", mysql_error(conn));     exit(1);   }
      char *tell="SQL Table Query... ";   printf(tell);   // SQL 普通表查询   char *sql="select password from Users whereUserName='client1@192.168.1.122'";   if (mysql_query(conn, sql)) {     fprintf(stderr, "%s ", mysql_error(conn));     exit(1);   }   res = mysql_use_result(conn);   printf("SqlCommand:%s",sql);   printf(" ");   while ((row = mysql_fetch_row(res)) != NULL) {     printf("PassWord:%s ", row[0]);   }   mysql_free_result(res);
      char *tell2="SQL Store Query More... ";   printf(tell2);   // SQL 存储过程查询   char *sql1="call P_GetCurrentCostPriceByUserName('client1@192.168.1.122')";   if (mysql_query(conn, sql1)) {     fprintf(stderr, "%s ", mysql_error(conn));     exit(1);   }   
      /*    * 存储过程默认返回的是多个结果集,    * 所以要用mysql_next_result取出并检查    * 下一个的结果集。否则在存储过程下的    * 其它查询语句都会出现 “Commands out of sync;    * you can't run this command now”错误!   */   do {     if ((res = mysql_use_result(conn))) {       printf("SqlCommand:%s",sql1);       printf(" ");       while ((row = mysql_fetch_row(res)) != NULL) {         printf("UserName:%s ", row[0]);         printf("Balance:%s ",row[1]);         printf("Price:%s ",row[2]);       }     }   }while (!mysql_next_result(conn));   mysql_free_result(res);
      char *tell3="SQL View Query More... ";   printf(tell3);   // SQL 视图查询   char *sql2="select CameraID,URL,RtspName,PW,PTZ,PTZServer from V_UserEquipment whereLoginName='client1@192.168.1.122'";   if (mysql_query(conn, sql2)) {     fprintf(stderr, "%s ", mysql_error(conn));     exit(1);   }   res = mysql_use_result(conn);   printf("SqlCommand:%s",sql2);   printf(" ");   while ((row = mysql_fetch_row(res)) != NULL) {     printf("CameraID:%s ", row[0]);     printf("URL:%s ",row[1]);     printf("RtspName:%s ",row[2]);     printf("PW:%s ", row[3]);     printf("PTZ:%s ",row[4]);     printf("PTZServer:%s ",row[5]);   }   mysql_free_result(res);
      mysql_close(conn);
      char *end="+--------------END----------------+ ";   printf(end); }
    复制代码

    编译:

    gcc -o sqla $(mysql_config --cflags) sqla.c $(mysql_config --libs)

    运行结果:

    复制代码
    +--------------BEGIN---------------+
    SQL Table Query...
    SqlCommand:select password from Users where UserName='client1@192.168.1.122'
    PassWord:client1 
    SQL Store Query More...
    SqlCommand:call P_GetCurrentCostPriceByUserName('client1@192.168.1.122')
    UserName:client1@192.168.1.122 
    Balance:30000 
    Price:0.05 
    SQL View Query More...
    SqlCommand:select CameraID,URL,RtspName,PW,PTZ,PTZServer from V_UserEquipment where LoginName='client1@192.168.1.122'
    CameraID:051010049@192.168.1.122_0 
    [url=rtsp://192.168.1.93/1.mp4]URL:rtsp://192.168.1.93/1.mp4[/url] 
    RtspName:admin 
    PW:admin 
    PTZ:1 
    PTZServer:ptzserver1@192.168.1.122 
    +--------------END----------------+
    复制代码
  • 相关阅读:
    关闭各种浏览器自动更新的方法
    新中新question
    linux忘记root密码后的解决办法
    Linux下的命令
    windows未启用Administrator账户
    Jenkins安装以及邮件配置
    pl/sql不能复制粘贴
    python-装饰器&自动化框架搭建
    进程练习——生成指定规格的图片
    python-类的各种方法
  • 原文地址:https://www.cnblogs.com/weizhixiang/p/6523934.html
Copyright © 2011-2022 走看看