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

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

[c/c++] c 操作mysql數(shù)據(jù)庫

系統(tǒng) 3296 0

[c/c++] c 操作mysql數(shù)據(jù)庫 - bluefrog - 博客園

[c/c++] c 操作mysql數(shù)據(jù)庫

輸出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)
          

結(jié)果

            $ ./
            
              version
mysql client version:
            
            
              5.1
            
            .
            
              63
            
          

?

創(chuàng)建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
            
             }
          

創(chuàng)建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); 
            
              //
            
            
               記錄項數(shù)
            
            
              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數(shù)據(jù)庫


更多文章、技術(shù)交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 兴业县| 资阳市| 高陵县| 驻马店市| 乐陵市| 微山县| 东海县| 新民市| 汉阴县| 监利县| 桃江县| 治县。| 海宁市| 建湖县| 苍梧县| 延庆县| 漾濞| 麟游县| 林周县| 囊谦县| 蓬安县| 闽清县| 尼勒克县| 阿瓦提县| 临海市| 桓仁| 鸡西市| 花莲县| 临沧市| 新巴尔虎左旗| 奎屯市| 洱源县| 甘孜县| 林周县| 廉江市| 临澧县| 木兰县| 读书| 连城县| 濮阳市| 文安县|