2#include3#include4#include56intmain(intargc,char**argv){7printf("mysqlclientversion:%s\n",mysql_get_client_info());8retur" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

[c/c++] c 操作mysql數據庫

系統 2936 0

[c/c++] c 操作mysql數據庫 - bluefrog - 博客園

[c/c++] c 操作mysql數據庫

輸出mysql版本

            
              1
            
             #include <my_global.h>

            
              2
            
             #include <mysql.h>

            
              3
            
             #include <stdlib.h>

            
              4
            
             #include <stdio.h>

            
              5
            
            
              6
            
            
              int
            
             main(
            
              int
            
             argc,
            
              char
            
             **
            
              argv) {

            
            
              7
            
                 printf(
            
              "
            
            
              mysql client version:%s\n
            
            
              "
            
            
              ,mysql_get_client_info());

            
            
              8
            
            
              return
            
            
              0
            
            
              ;

            
            
              9
            
             }
          

編譯

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

結果

            $ ./
            
              version
mysql client version:
            
            
              5.1
            
            .
            
              63
            
          

?

創建DB

            
               1
            
             #include <my_global.h>

            
               2
            
             #include <mysql.h>

            
               3
            
             #include <stdio.h>

            
               4
            
             #include <stdlib.h>

            
               5
            
            
               6
            
            
              int
            
             main(
            
              int
            
             argc,
            
              char
            
             **
            
              argv) {

            
            
               7
            
                 MYSQL *
            
              conn;

            
            
               8
            
            
               9
            
                 conn =
            
               mysql_init(NULL);

            
            
              10
            
            
              if
            
            (conn ==
            
               NULL) {

            
            
              11
            
                     printf(
            
              "
            
            
              Error %u:%s\n
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              12
            
            
                      exit(EXIT_FAILURE);

            
            
              13
            
            
                  }

            
            
              14
            
            
              15
            
            
              //
            
            
               host user password 
            
            
              16
            
            
              if
            
            (mysql_real_connect(conn,
            
              "
            
            
              localhost
            
            
              "
            
            ,
            
              "
            
            
              root
            
            
              "
            
            ,
            
              "
            
            
              admin
            
            
              "
            
            ,NULL,
            
              0
            
            ,NULL,
            
              0
            
            ) ==
            
               NULL) {

            
            
              17
            
                     printf(
            
              "
            
            
              Error %u:%s\n
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              18
            
            
                      exit(EXIT_FAILURE);

            
            
              19
            
            
                  }

            
            
              20
            
            
              21
            
            
              char
            
            * sql = 
            
              "
            
            
              CREATE DATABASE IF NOT EXISTS test_cdb
            
            
              "
            
            
              ;

            
            
              22
            
            
              //
            
            
              char* sql = "CREATE database test_cdb";
            
            
              23
            
            
              if
            
            
              (mysql_query(conn,sql)) {

            
            
              24
            
                     printf(
            
              "
            
            
              Error %u:%s\n
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              25
            
            
                      exit(EXIT_FAILURE);

            
            
              26
            
            
                  }

            
            
              27
            
            
              28
            
            
                  mysql_close(conn);

            
            
              29
            
            
              30
            
            
                  exit(EXIT_SUCCESS);

            
            
              31
            
             }
          

創建Table

            
               1
            
             #include <my_global.h>

            
               2
            
             #include <mysql.h>

            
               3
            
             #include <stdio.h>

            
               4
            
             #include <stdlib.h>

            
               5
            
            
               6
            
            
              int
            
             main(
            
              int
            
             argc,
            
              char
            
             **
            
              argv) {

            
            
               7
            
                 MYSQL *
            
              conn;

            
            
               8
            
            
               9
            
                 conn =
            
               mysql_init(NULL);

            
            
              10
            
            
              //
            
            
               host user password dbname
            
            
              11
            
            
              if
            
            (mysql_real_connect(conn,
            
              "
            
            
              localhost
            
            
              "
            
            ,
            
              "
            
            
              root
            
            
              "
            
            ,
            
              "
            
            
              admin
            
            
              "
            
            ,
            
              "
            
            
              test_cdb
            
            
              "
            
            ,
            
              0
            
            ,NULL,
            
              0
            
            ) ==
            
               NULL) {

            
            
              12
            
                     printf(
            
              "
            
            
              Error %u:%s
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              13
            
            
                      exit(EXIT_FAILURE);

            
            
              14
            
            
                  }

            
            
              15
            
            
              16
            
            
              char
            
            * sql = 
            
              "
            
            
              CREATE TABLE IF NOT EXISTS test(name VARCHAR(25));
            
            
              "
            
            
              ;

            
            
              17
            
            
              if
            
            
              (mysql_query(conn,sql)) {

            
            
              18
            
                     printf(
            
              "
            
            
              Error %u:%s
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              19
            
            
                      exit(EXIT_FAILURE);

            
            
              20
            
            
                  }

            
            
              21
            
                 sql = 
            
              "
            
            
              INSERT INTO test VALUES('test1')
            
            
              "
            
            
              ;

            
            
              22
            
            
              if
            
            
              (mysql_query(conn,sql)) {

            
            
              23
            
                     printf(
            
              "
            
            
              Error %u:%s
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              24
            
            
                      exit(EXIT_FAILURE);

            
            
              25
            
            
                  }

            
            
              26
            
            
              27
            
            
                  mysql_close(conn);

            
            
              28
            
            
                  exit(EXIT_SUCCESS);

            
            
              29
            
             }
          

查詢

            
               1
            
             #include <my_global.h>

            
               2
            
             #include <mysql.h>

            
               3
            
             #include <stdio.h>

            
               4
            
             #include <stdlib.h>

            
               5
            
            
               6
            
            
              int
            
             main(
            
              int
            
             argc,
            
              char
            
             **
            
              argv) {

            
            
               7
            
                 MYSQL *
            
              conn;

            
            
               8
            
                 MYSQL_RES *
            
              result;

            
            
               9
            
            
                  MYSQL_ROW row;

            
            
              10
            
                 MYSQL_FIELD *
            
              field;

            
            
              11
            
            
              12
            
            
              int
            
            
               num_fields;

            
            
              13
            
            
              int
            
            
               i;

            
            
              14
            
            
              int
            
             j = 
            
              0
            
            
              ;

            
            
              15
            
            
              16
            
                 conn =
            
               mysql_init(NULL);

            
            
              17
            
            
              if
            
            (mysql_real_connect(conn,
            
              "
            
            
              localhost
            
            
              "
            
            ,
            
              "
            
            
              root
            
            
              "
            
            ,
            
              "
            
            
              admin
            
            
              "
            
            ,
            
              "
            
            
              test_cdb
            
            
              "
            
            ,
            
              0
            
            ,NULL,
            
              0
            
            ) ==
            
               NULL) {

            
            
              18
            
                     printf(
            
              "
            
            
              Error %u:%s
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              19
            
            
                      exit(EXIT_FAILURE);

            
            
              20
            
            
                  }

            
            
              21
            
            
              22
            
            
              char
            
            * sql = 
            
              "
            
            
              SELECT * FROM test
            
            
              "
            
            
              ;

            
            
              23
            
            
              if
            
            
              (mysql_query(conn,sql)) {

            
            
              24
            
                     printf(
            
              "
            
            
              Error %u:%s
            
            
              "
            
            
              ,mysql_errno(conn),mysql_error(conn));

            
            
              25
            
            
                      exit(EXIT_FAILURE);

            
            
              26
            
            
                  }

            
            
              27
            
                 result =
            
               mysql_store_result(conn);

            
            
              28
            
                 num_fields = mysql_num_fields(result); 
            
              //
            
            
               記錄項數
            
            
              29
            
            
              30
            
            
              while
            
            ((row =
            
               mysql_fetch_row(result))) {

            
            
              31
            
            
              //
            
            
               for(int i = 0; i < num_fields;i++) { 
            
            
              //
            
            
               allowed c99 mode
            
            
              32
            
            
              for
            
            (i = 
            
              0
            
            ; i < num_fields;i++
            
              ) {

            
            
              33
            
            
              if
            
            (j == 
            
              0
            
            
              ) {

            
            
              34
            
            
              //
            
            
               struct ?
            
            
              35
            
            
              while
            
            (field =
            
               mysql_fetch_field(result)) {

            
            
              36
            
                                 printf(
            
              "
            
            
              %s 
            
            
              "
            
            ,field->
            
              name);

            
            
              37
            
            
                              }

            
            
              38
            
                             printf(
            
              "
            
            
              \n
            
            
              "
            
            
              );

            
            
              39
            
            
                          }

            
            
              40
            
                         printf(
            
              "
            
            
              %s 
            
            
              "
            
            ,row[i]? row[i] : 
            
              "
            
            
              NULL
            
            
              "
            
            
              );

            
            
              41
            
            
                      }

            
            
              42
            
                     printf(
            
              "
            
            
              \n
            
            
              "
            
            
              );

            
            
              43
            
                     j++
            
              ;

            
            
              44
            
            
                  }

            
            
              45
            
            
                  mysql_free_result(result);

            
            
              46
            
            
              47
            
            
                  mysql_close(conn);

            
            
              48
            
            
                  exit(EXIT_SUCCESS);

            
            
              49
            
             }
          

?

?

[c/c++] c 操作mysql數據庫


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲精视频 | 男女超猛烈啪啦啦的免费视频 | 亚洲AV久久久噜噜噜久久 | 日本黄色大片免费看 | 精品国产一区在线观看 | 国产精品乱码一区二三区小蝌蚪 | 一区在线免费观看 | 色婷婷在线播放 | 高清视频在线观看 免费 | 5060网午夜 | 中文字幕一区二区三区四区不卡 | 新久草视频 | 九九精品激情在线视频 | 青青草华人在线 | 欧美高清成人 | 成人涩涩屋福利视频 | av在线国产精品 | 国产精品久久久久久日本一道 | 色婷婷成人做爰A片免费看网站 | 中文字幕一区二区三区乱码图片 | 久久成人国产精品免费 | 日韩av在线中文字幕 | 日本理论片中文在线观看2828 | 久草福利在线视频 | 丝袜美腿一区二区三区 | 精品欧美一区二区三区精品久久 | 夫妻性生活交换 | 亚洲国产资源 | 国产精品久久一区 | 無码一区中文字幕少妇熟女H | 蜜臀AV国产精品久久久久 | 91视频视频| 日韩精品一区在线观看 | 国产一区二区精品丝袜 | 日韩v在线| 成人黄视频在线观看 | 欧美三级三级三级爽爽爽 | 成人97在线观看免费高清 | 日韩视频中文字幕 | 九九视频高清视频免费观看 | 国产精品一区二区免费 |