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

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統 2999 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條評論
主站蜘蛛池模板: 平陆县| 怀来县| 乌兰浩特市| 汶川县| 安图县| 洛宁县| 凉城县| 华阴市| 石城县| 大庆市| 峨眉山市| 嘉善县| 平昌县| 邵武市| 楚雄市| 葫芦岛市| 丽江市| 紫云| 岢岚县| 宁远县| 阿克陶县| 闵行区| 湘阴县| 内黄县| 清镇市| 株洲县| 湖口县| 康保县| 宽甸| 荃湾区| 白河县| 隆昌县| 兴业县| 镇远县| 潮州市| 虹口区| 德格县| 左云县| 象州县| 黔南| 奉新县|