在VS中如何用C++串連Mysql

來源:互聯網
上載者:User

標籤:

在如鵬網上看到的如何用C串連Mysql,解決了大二時的一直困惑,大喜!

第一步下載

安裝的資料庫是如鵬網的Mysql :http://pan.baidu.com/s/1c0m3xIw 提取碼:m9sn)

儲存在我的網盤 (MySQL Server 5.6免安裝版.zip): http://pan.baidu.com/s/1jG4KZ8y

 

第二步安裝

綠色版MYSQL Server的安裝:

1)解壓到沒有中文、空格的檔案夾下

2)雙擊mysqld.exe,如果進程中有了mysqld.exe就ok了

3)上面的方式需要每次重啟電腦都要手動運行,特別是如果運行在伺服器上,那麼不能在登入前就啟動。可以註冊為系統服務:以管理員身份運行命令列,cd到mysql的bin檔案夾,執行“mysqld -install”;卸載服務:mysqld -remove。

*綠色版MYSQL的使用者名稱、密碼都是:root

 

第三步安裝管理工具

資料庫管理工具 (Navicat Lite) V9.1.11 

:http://www.cr173.com/soft/44816.html

*Navicat與Mysql的對接:http://www.rupeng.com/Segments/Index/1800

第四步環境搭建

來源(http://www.rupeng.com/Segments/Index/1896)

  1. 在項目屬性中【VC++目錄】→“包含目錄”,選擇mysql的include檔案夾;
  2. “庫目錄”選擇mysql的lib檔案夾;
  3. 【連結器】→【輸入】的“附加依賴項”增加“libmysql.lib”。
  4. 編譯運行,會報錯找不到“libmysql.dll”,把mysql的libmysql.dll複製到exe的目錄下。

 

第五步測試下

測試代碼:

#include <stdlib.h>#include <stdio.h>#include <winsock.h>#include <mysql.h> int main(){   MYSQL *mysql = mysql_init(0);   return 0;}
View Code
#include <stdlib.h>#include <stdio.h>#include <winsock.h>#include <mysql.h> int main(){MYSQL *mysql = mysql_init(0);    if(!mysql_real_connect(mysql,"localhost","root","root","study3",0,0,0))    {        printf("串連資料庫出錯:%s",mysql_error(mysql));               goto exit;//goto一般不推薦使用,但是在錯誤處理的時候,很好用        //沒有絕對好的東西,沒有絕對壞的東西。就看用這個技術的人的水平怎麼樣    }    printf("串連資料庫成功!\n");    if(mysql_query(mysql,"set names gbk"))    {        printf("設定串連編碼失敗%s",mysql_error(mysql));        goto exit;    }     if(mysql_query(mysql,"insert into T_Users(UserName,Password) values(‘我是中文‘,‘aaa123‘)"))    {        printf("插入失敗,%s",mysql_error(mysql));        goto exit;    }    printf("insert成功\n");exit:    mysql_close(mysql);//程式最後必須關閉串連,否則會有mysql伺服器串連過多卡死的可能性    printf("exit");    getchar();    return 0;}
View Code

沒有報錯就是串連成功!

 

 

大喜,願望實現!!

來源:(http://www.rupeng.com/Segments/Index/1896)

在VS中如何用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.