c++串連mysql資料庫

來源:互聯網
上載者:User

標籤:管理   代碼   pass   from   select   ibm   host   connect   turn   

1.使用VS建立項目

2.右鍵項目選擇屬性,進入C/C++ => 常規 => 附加元件封裝含目錄,添加D:\mysql-5.7.19-win64\include(根據自己安裝的MySQL版本及地址填寫)

3.在屬性頁面,進入連結器 => 常規 => 附加庫目錄,添加D:\mysql-5.7.19-winx64\lib(根據自己情況填寫)

4.在項目的.\x64\Debug\中添加libmysql.dll(從電腦的資源管理員中添加),該dll檔案在D:\mysql-5.7.19-winx64\lib目錄下

5.代碼測試

#include "stdio.h"#include "mysql.h"int main(){    MYSQL * con; //= mysql_init((MYSQL*) 0);     MYSQL_RES *res;    MYSQL_ROW row;    char tmp[400];    //database configuartion    char dbuser[30] = "root";  //mysql帳號名    char dbpasswd[30] = "123456"; //mysql帳號密碼    char dbip[30] = "localhost";    char dbname[50] = "student";//資料庫名    char tablename[50] = "bl"; //資料表名    char *query = NULL;    int x;    int y;    int rt;//return value      unsigned int t;    int count = 0;    con = mysql_init((MYSQL*)0);    if (con != NULL && mysql_real_connect(con, dbip, dbuser, dbpasswd, dbname, 3306, NULL, 0)) {        if (!mysql_select_db(con, dbname)) {            printf("Select successfully the database!\n");            con->reconnect = 1;            query = "set names \‘GBK\‘";            rt = mysql_real_query(con, query, strlen(query));            if (rt) {                printf("Error making query: %s !!!\n", mysql_error(con));            }            else {                printf("query %s succeed!\n", query);            }        }    }    else {        MessageBoxA(NULL, "Unable to connect the database,check your configuration!", "", NULL);    }    //sprintf(tmp, "insert into %s values(%s,%d,%d)", tablename, "null", x, y); //注意如何向具有自增欄位的資料庫中插入記錄    sprintf(tmp, "insert into bl values(null,‘x‘,‘x‘,‘x‘,‘x‘)");    rt = mysql_real_query(con, tmp, strlen(tmp));    if (rt)    {        printf("Error making query: %s !!!\n", mysql_error(con));    }    else    {        printf("%s executed!!!\n", tmp);    }    sprintf(tmp, "select * from %s", tablename);    rt = mysql_real_query(con, tmp, strlen(tmp));    if (rt)    {        printf("Error making query: %s !!!\n", mysql_error(con));    }    else    {        printf("%s executed!!!\n", tmp);    }    res = mysql_store_result(con);//將結果儲存在res結構體中    while (row = mysql_fetch_row(res)) {        for (t = 0; t<mysql_num_fields(res); t++) {            printf("%s  ", row[t]);        }        printf(".............\n");        count++;    }    printf("number of rows %d\n", count);    printf("mysql_free_result...\n");    mysql_free_result(res);    mysql_close(con);    system("pause");    return 0;}

c++串連mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.