首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

C/C++ 直連 MySQL 高效開發指南:繞過 ODBC、Windows NT 平台實戰

     編輯:關於VC++

專案背景

股市即時行情系統需每分鐘更新 1000+ 支股票資料,同時支援 50+ 併發查詢。MS SQL Server + ODBC 效能不足,改用 MySQL C API 直接存取,單台 PII 350/128MB/SCSI 即達每秒 70-100 筆寫入 + 50 併發查詢,成本大幅降低。

MySQL C API 核心優勢

  • 零中間層:直接呼叫 libmysql.dll,無 ODBC 轉譯開銷
  • 跨平台:原始碼級相容 Linux/Unix/Windows
  • 基準測試領先:讀取 200 萬筆索引資料 MySQL 367 秒 vs SQL Server 1634 秒 (ODBC)
  • 免費開源 (GPL),商業授權彈性

Windows NT 安裝與配置

  1. 下載 mysql-shareware-3.22.32-win.zip (或新版 Community Server)
  2. 執行 Setup 安裝至 C:\mysql
  3. 複製 my-example.cnf 為 C:\my.cnf,按需調整
  4. 啟動服務:C:\mysql\bin\mysqld-shareware --standalone
  5. 建立管理員密碼:mysqladmin -u root password your_pass
  6. 刪除匿名帳號:mysql -u root -p -e "DELETE FROM user WHERE Host='localhost' AND User=''; FLUSH PRIVILEGES;"

VC++ 專案配置

  • 專案屬性 → C/C++ → 一般 → 附加包含目錄:C:\mysql\include
  • 連結器 → 一般 → 附加程式庫目錄:C:\mysql\lib
  • 連結器 → 輸入 → 附加相依性:libmysql.lib
  • 複製 libmysql.dll 至 C:\WinNT\System32 或專案輸出目錄

核心 API 快速入門

完整範例 (Console App)


#include 
#include 
#include 

int main() {
    MYSQL* conn = mysql_init(NULL);
    if (!conn) return 1;

    // 連線
    if (!mysql_real_connect(conn, "localhost", "root", "your_pass", "test", 3306, NULL, 0)) {
        fprintf(stderr, "連線失敗: %s\n", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }

    // 建表
    if (mysql_query(conn, "CREATE TABLE mytable (time DATETIME, s1 CHAR(6), s2 CHAR(11), s3 INT, s4 INT)")) {
        fprintf(stderr, "建表失敗: %s\n", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }

    // 插入
    if (mysql_query(conn, "INSERT INTO mytable VALUES('2000-03-10 21:01:30','Test','MySQLTest',2000,3)")) {
        fprintf(stderr, "插入失敗: %s\n", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }

    // 查詢
    if (mysql_query(conn, "SELECT * FROM mytable")) {
        fprintf(stderr, "查詢失敗: %s\n", mysql_error(conn));
        mysql_close(conn);
        return 1;
    }

    MYSQL_RES* res = mysql_store_result(conn);
    int nFields = mysql_num_fields(res);
    MYSQL_FIELD* fields = mysql_fetch_fields(res);

    // 列印欄位名
    for (int i = 0; i < nFields; ++i) printf("%s\t", fields[i].name);
    printf("\n");

    // 列印資料
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(res))) {
        for (int i = 0; i < nFields; ++i)
            printf("%s\t", row[i] ? row[i] : "NULL");
        printf("\n");
    }

    mysql_free_result(res);
    mysql_close(conn);
    return 0;
}

效能優化關鍵

  1. 使用預處理語句 (mysql_stmt_prepare):減少 SQL 解析開銷,防 SQL 注入
  2. 批次插入:單次 INSERT ... VALUES (...), (...), ... 寫入多筆
  3. 連線池:應用層維護連線池,避免頻繁 connect/close
  4. 適當索引:查詢欄位建立 B-Tree 索引
  5. 調整 my.cnf:key_buffer_size, table_cache, sort_buffer_size 依記憶體調大

跨平台移植提示

  • 原始碼無需修改即可在 Linux/Unix 編譯 (連結 -lmysqlclient)
  • Windows 下亦可使用 MyODBC 驅動搭配標準 ODBC API,犧牲些效能換取程式碼通用性

總結

MySQL C API 以極致效能、零授權成本、跨平台能力,成為 Windows NT 平台高併發資料庫應用的最佳選擇。配合合理的連線池、預處理語句與索引設計,輕鬆支撐百萬級資料量與高併發存取。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
函式用途
mysql_init()初始化 MYSQL 結構體
mysql_real_connect()建立資料庫連線
mysql_query()執行 SQL (非查詢)
mysql_store_result()取得完整結果集 (緩衝模式)
mysql_fetch_row()逐列讀取資料
mysql_fetch_field()取得欄位中繼資料
mysql_free_result()釋放結果集
mysql_close()關閉連線