C/C++鏈接MySQL數據庫執行查詢

1. 簡介:html

使用C/C++鏈接MySQL數據庫執行增刪改查操做,基本就是圍繞如下兩個文件展開:mysql

  • mysql.h(此頭文件通常在MySQL的include文件夾內,如 D:\MySQL\mysql-5.7.23-winx64\include)
  • MySQL官方 C API

C/C++鏈接數據庫執行查詢操做時須要將sql查詢語句嵌入到mysql_query語句中,具體見API中的mysql_query()函數git

 

2. 數據庫安裝github

本文代碼是以MySQL官方實例數據庫employees爲對象進行的查詢,若是想直接運行下述代碼,須要安裝employees數據庫,固然也能夠使用下述代碼對其餘數據庫進行查詢,只須要修改代碼中的部分sql語句。下面介紹一下employees數據庫的安裝:sql

employees下載:(一下兩種方法均可以,任選其一,兩方法獲得的文件相同test_db-master.zip)數據庫

employees安裝:api

 

3. 完整代碼數組

/*
C/C++鏈接MySQL數據庫時,須要包含一個*.h的mysql頭文件和一個mysql的lib文件
一、初始化;二、鏈接數據庫;三、執行sql查詢語句;四、獲取查詢值;五、關閉
*/
#include <stdio.h>
#include <WinSock.h>  
#include <mysql.h>   
#include <Windows.h>
#pragma comment(lib,"wsock32.lib")
#pragma comment(lib,"libmysql.lib")

MYSQL mysql;
MYSQL_FIELD *fd;    //字段列數組
char field[32][32]; //存字段名二維數組
MYSQL_RES *res;     //行的一個查詢結果集
MYSQL_ROW column;   //數據行的列
char query[150];    //查詢語句

//函數聲明
bool ConnectDatabase();     
void FreeConnect();
bool QueryDatabase(); 
bool InsertData();
bool ModifyData();
bool DeleteData();

int main(int argc, char **argv){
    ConnectDatabase();
    QueryDatabase();
    InsertData();
    QueryDatabase();
    ModifyData();
    QueryDatabase();
    //DeleteData();
    //QueryDatabase();
    FreeConnect();
    system("pause");
    return 0;
}

//鏈接數據庫
bool ConnectDatabase(){
    //Gets or initializes a MYSQL structure
    mysql_init(&mysql); 

    // Connects to a MySQL server
    const char host[] = "localhost";
    const char user[] = "root";
    const char passwd[] = "root";
    const char db[] = "employees";
    unsigned int port = 3306;
    const char *unix_socket = NULL;
    unsigned long client_flag = 0;

    /*A MYSQL* connection handler if the connection was successful, 
    NULL if the connection was unsuccessful. For a successful connection, 
    the return value is the same as the value of the first parameter.*/
    if (mysql_real_connect(&mysql, host, user, passwd, db, port, unix_socket, client_flag)){
        printf("The connection was successful.\n");
        return true;
    }
    else{
        /*const char *mysql_error(MYSQL *mysql)
        Returns the error message for the most recently invoked MySQL function
        A null-terminated character string that describes the error. 
        An empty string if no error occurred.*/
        printf("Error connecting to database:%s\n", mysql_error(&mysql));
        return false;
    }
}

//釋放資源
/*void mysql_free_result(MYSQL_RES *result)
Frees the memory allocated for a result set by mysql_store_result(), 
mysql_use_result(), mysql_list_dbs(), and so forth.When you are done 
with a result set, you must free the memory it uses by calling mysql_free_result().
Do not attempt to access a result set after freeing it.*/

/*void mysql_close(MYSQL *mysql)
Closes a previously opened connection.mysql_close() also deallocates 
the connection handler pointed to by mysql if the handler was allocated 
automatically by mysql_init() or mysql_connect().*/
void FreeConnect(){
    mysql_free_result(res);
    mysql_close(&mysql);
}

//查詢數據
bool QueryDatabase(){
    //將數據格式化輸出到字符串
    sprintf_s(query, "select * from departments");
    //設置編碼格式
    mysql_query(&mysql, "set names gbk"); 

    /*int mysql_query(MYSQL *mysql, const char *stmt_str)
    Executes an SQL query specified as a null-terminated string
    Executes the SQL statement pointed to by the null-terminated string stmt_str. 
    Normally, the string must consist of a single SQL statement without 
    a terminating semicolon (;). If multiple-statement execution has been enabled,
    the string can contain several statements separated by semicolons.
    Return Values:Zero for success. Nonzero if an error occurred.*/
    if (mysql_query(&mysql, query)){
        printf("Query failed (%s)\n", mysql_error(&mysql));
        return false;
    }
    else{
        printf("query success\n");
    }

    /*MYSQL_RES *mysql_store_result(MYSQL *mysql)
    Retrieves a complete result set to the client
    mysql_store_result() reads the entire result of a query to the client,
    allocates a MYSQL_RES structure, and places the result into this structure. 
    mysql_store_result() returns a null pointer if the statement did not return 
    a result set(for example, if it was an INSERT statement). mysql_store_result() 
    also returns a null pointer if reading of the result set failed.
    You can check whether an error occurred by checking whether mysql_error() 
    returns a nonempty string. Return Values:A MYSQL_RES result structure with
    the results.NULL(0) if an error occurred.*/
    res = mysql_store_result(&mysql);
    if (!res){
        printf("Couldn't get result from %s\n", mysql_error(&mysql));
        return false;
    }

    /*my_ulonglong mysql_affected_rows(MYSQL *mysql)
    It returns the number of rows changed, deleted, 
    or inserted by the last statement if it was an UPDATE, DELETE, or INSERT. 
    For SELECT statements, returns the number of rows in the result set.*/
    printf("number of dataline returned: %d\n", mysql_affected_rows(&mysql));

    /*MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result)
    Returns the definition of one column of a result set as a MYSQL_FIELD structure.
    Call this function repeatedly to retrieve information about all columns in the result set.*/
    
    // 獲取列數
    int j = mysql_num_fields(res);

    //存儲字段信息
    char *str_field[32];  

    //獲取字段名
    for (int i = 0; i < j; i++){ 
        str_field[i] = mysql_fetch_field(res)->name;
    }

    //打印字段
    for (int i = 0; i < j; i++)  
        printf("%10s\t", str_field[i]);
    printf("\n");

    //打印查詢結果
    //MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
    //Fetches the next row from the result set
    while (column = mysql_fetch_row(res)){
        printf("%10s\t%10s\n", column[0], column[1]);
    }
    return true;
}

//插入數據
bool InsertData(){
    sprintf_s(query, "insert into departments values ('xxxx', 'xxxxx');");
    if (mysql_query(&mysql, query)) {
        printf("Query failed (%s)\n", mysql_error(&mysql));
        return false;
    }
    else{
        printf("Insert success\n");
        return true;
    }
}

//修改數據
bool ModifyData(){
    sprintf_s(query, "update departments set dept_name='yyyyy' where dept_no='xxxx'");
    if (mysql_query(&mysql, query)) {
        printf("Query failed (%s)\n", mysql_error(&mysql));
        return false;
    }
    else{
        printf("Insert success\n");
        return true;
    }
}

//刪除數據
bool DeleteData()
{
    sprintf_s(query, "delete from departments where dept_no='xxxx';");
    if (mysql_query(&mysql, query)) {
        printf("Query failed (%s)\n", mysql_error(&mysql));
        return false;
    }
    else{
        printf("Insert success\n");
        return true;
    }
}
相關文章
相關標籤/搜索