MySQL 數據庫應用程序編程


普通用戶使用客戶端應用程序(Client Application)和服務器程序(Server Application)通信以取得服務, 而服務器程序通常要和數據庫服務器通信以取得數據存取服務, 這時就需要使用到使用到數據庫連接技術, 我本人將使用數據庫連接技術編程, 稱為數據庫應用程序編程, 更多時候的數據庫應用程序編程依賴數據庫發行商提供的驅動程序或接口模塊.

MySQL 為程序員提供了多種編程語言的接口, 如使用 C 語言編程, 則可以下載 MySQL Connector C 套件, 解壓后得到 include 目錄下的頭文件, 和 lib 目錄下的共態鏈接庫 libmysql.dll, 該動態鏈接庫 的導入庫libmysql.lib, 以及靜態對象庫 mysqlclient.lib, 顯然, 使用動態鏈接庫的編譯速度更快.

 

關於這些 API 的使用方法, 請參考官方文檔, 下面是一個演示.

 

 

  1 #include <Windows.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <tchar.h>
  5 #include <assert.h>
  6 #include <C:\Program Files\MySQL\MySQL Connector C 6.1\include\mysql.h>
  7 
  8 #pragma comment(lib, "C:\\Program Files\\MySQL\\MySQL Connector C 6.1\\lib\\libmysql.lib")
  9 
 10 void Todo(MYSQL *mysql) {
 11 
 12     const char *a = mysql_stat(mysql);
 13     printf("數據庫連接狀態:\n"
 14         "%s\n", a);
 15 
 16     // 創建一個數據庫
 17     char cmd[1024];
 18     sprintf(cmd, "CREATE DATABASE `my_test_db`");
 19 
 20     if (mysql_query(mysql, cmd) != EXIT_SUCCESS) {
 21         printf("創建失敗!\n"
 22             "%d: %s\n", mysql_errno(mysql), mysql_error(mysql));
 23     } else {
 24         printf("創建成功!\n");
 25     }
 26 
 27     // 選擇數據庫
 28     sprintf(cmd, "USE `my_test_db`");
 29     if (mysql_query(mysql, cmd) != EXIT_SUCCESS) {
 30         printf("選擇失敗!\n"
 31             "%d: %s\n", mysql_errno(mysql), mysql_error(mysql));
 32         return ;
 33     } else {
 34         printf("選擇了表!\n");
 35     }
 36 
 37     // 創建表
 38     sprintf(cmd, "CREATE TABLE `t`("
 39         "ID INT NOT NULL AUTO_INCREMENT,"
 40         "Name VARCHAR(20) NOT NULL,"
 41         "PRIMARY KEY(ID)"
 42         ")");
 43     if (mysql_query(mysql, cmd) != EXIT_SUCCESS) {
 44         printf("創建失敗!\n"
 45             "%d: %s\n", mysql_errno(mysql), mysql_error(mysql));
 46     }
 47 
 48     // 查看表型
 49     sprintf(cmd, "DESC `t`");
 50     if (mysql_query(mysql, cmd) != EXIT_SUCCESS) {
 51 
 52         printf("查看失敗!\n"
 53             "%d: %s\n", mysql_errno(mysql), mysql_error(mysql));
 54 
 55     } else {
 56 
 57         // 取結果集
 58         MYSQL_RES *res;
 59         res = mysql_store_result(mysql);
 60 
 61         if (res == NULL) {
 62             printf("查詢沒有返回結果\n");
 63         } else {
 64 
 65             int fc = mysql_field_count(mysql);
 66             printf("字段數量: %u.\n", fc);
 67             // 一行一行地取結果
 68             MYSQL_ROW row;
 69             while (row = mysql_fetch_row(res)) {
 70                 int i = 0;
 71                 for (; i < fc; i++) {
 72                     printf("|%s\t", row[i] != NULL ? row[i] : "NULL");
 73                 }
 74                 printf("\n");
 75             }
 76         }
 77     }
 78 }
 79 
 80 void Test() {
 81 
 82     // 初始化 MySQL 客戶端模塊
 83     assert(mysql_library_init(0, NULL, NULL) == EXIT_SUCCESS);
 84 
 85     // 獲取數據庫描述符
 86     MYSQL *mysql = NULL;
 87     mysql = mysql_init(mysql);
 88     // 連接數據庫
 89     if (mysql_real_connect(mysql, "localhost", "root", "passwd", NULL, 3306, NULL, 0) == NULL) {
 90         printf("連接失敗!\n");
 91         printf("%d: %s\n", mysql_errno(mysql), mysql_error(mysql));
 92     }
 93 
 94     // 你想用數據庫做些什么事情?
 95     Todo(mysql);
 96 
 97     // 關閉數據庫連接
 98     mysql != 0 ? mysql_close(mysql) : 0;
 99     // 釋放模塊
100     mysql_library_end();
101 }
102 
103 int APIENTRY _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR lpszCmdLine, int iCmdShow) {
104 
105     // 分配控制台
106     AllocConsole();
107 
108     // 
109     Test();
110 
111     printf("\n"
112         "The end.\n");
113 
114     // 消息循環
115     MSG msg;
116     while (GetMessage(&msg, NULL, 0, 0) > 0) {
117         TranslateMessage(&msg);
118         DispatchMessage(&msg);
119     }
120 
121     return EXIT_SUCCESS;
122 }    

 

第一次運行:

再次運行:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM