codeblocks 下C++串連mysql

來源:互聯網
上載者:User

C++串連mysql資料庫

C++不像php那樣智能,沒有整合的wamp環境,因此,C++串連資料庫也就沒有那麼方便。上網查了些資料,終於串連成功了,記錄下過程 。

1.在電腦上安裝mysql

這一步網上有人說必須選擇custmo模式,不過我安裝時沒有選擇,也串連成功了。貌似沒有那個必要。

2.下載mysql標頭檔

這個 必須要有,下載好後,把裡面的.h檔案放到codeblocks的Include檔案夾下。這上步的目的是在寫程式中,可以引用mysql.h等標頭檔。

3.在codeblocks中的project -> build options ->setting and link ,添加,找到libmysql.lib,mysqlclient.lib,mysqld.lib這三個檔案,把這三個檔案的路徑全部加過去,目的是使codeblocks能夠找到並編譯。如果是預設安裝的話,這三個檔案所在的位置是C:\Program Files\MySQL\MySQL Server 5.1\lib,每個人的位置可能不一樣。

4.這時試著串連資料庫,如果不能夠串連成功,則可能會出現“沒有找到libmysql.dll,因此這個應用程式未能啟動。重新安裝應用程式可能會修複此問題。”解決方案:

4.1:將C:\mysql\lib\opt目錄下的libmysql.dll拷貝到 C:\WINDOWS\system32目錄下

4.2:將libmysql.dll拷貝到.exe所在的檔案下


最後附一個串連成功的代碼:

#include <iostream>#include <stdio.h>#include <winsock2.h>#include <mysql.h>using namespace std;/*資料庫連接用宏*/#define HOST "localhost"#define USERNAME "root"#define PASSWORD " "#define DATABASE "test"void query_sql(char* sql){    MYSQL my_connection; /*這是一個資料庫連接*/    int res; /*執行sql語句後的返回標誌*/    MYSQL_RES *res_ptr; /*指向查詢結果的指標*/    MYSQL_FIELD *field; /*欄位結構指標*/    MYSQL_ROW result_row; /*按行返回的查詢資訊*/    int row, column; /*查詢返回的行數和列數*/    int i, j;    /*初始化mysql串連my_connection*/    mysql_init(&my_connection);    /*建立mysql串連*/    if (NULL != mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD,                                   DATABASE, 0, NULL, 0))  /*串連成功*/    {        printf("資料庫查詢query_sql串連成功!\n");        /*設定查詢編碼為gbk,以支援中文*/        mysql_query(&my_connection, "set names gbk");        res = mysql_query(&my_connection, sql);        cout << "res = " << res << endl;        if (res)   /*執行失敗*/        {            printf("Error: mysql_query !\n");            cout << mysql_error(&my_connection) << endl;            /*關閉串連*/            mysql_close(&my_connection);        }        else     /*現在就代表執行成功了*/        {            /*將查詢的結果給res_ptr*/            res_ptr = mysql_store_result(&my_connection);            /*如果結果不為空白,就把結果print*/            if (res_ptr)            {                /*取得結果的行數和*/                column = mysql_num_fields(res_ptr);                row = mysql_num_rows(res_ptr);                printf("查詢到 %d 行 \n", row);                /*輸出結果的欄位名*/                for (i = 0; field = mysql_fetch_field(res_ptr); i++)                    printf("%10s ", field->name);                printf("\n");                /*按行輸出結果*/                for (i = 1; i < row+1; i++)                {                    result_row = mysql_fetch_row(res_ptr);                    for (j = 0; j < column; j++)                        printf("%10s ", result_row[j]);                    printf("\n");                }            }            /*不要忘了關閉串連*/            mysql_close(&my_connection);        }    }    else    {        printf("資料庫連接失敗");    }}int main(){    char *query;    query="select * from student";    query_sql(query);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.