C語言串連mysql資料庫

來源:互聯網
上載者:User

標籤:

作業系統:win7/64

編譯軟體:VS2010

資料庫:5.7.11

 

從C語言串連mysql資料庫包含兩個步驟:

1 初始化串連控制代碼結構

2 實際建立串連

 

測試代碼1:

#include "stdafx.h"#include <WinSock2.h> /*socket通訊,系統標頭檔*/#include <windows.h>#include <stdio.h>#include "mysql.h"#pragma comment(lib, "libmysql.lib")int main(){    MYSQL *conn;    char *server = "localhost";    char *user = "root";    char *password = "root";//“******”為你設定的密碼    char *database = "hyx";    conn = mysql_init(NULL);    if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))        printf("串連失敗:%s", mysql_error(conn));    else        printf("串連成功!");    getchar();    return 0;}

測試結果1:

 

遇到的問題:

error LNK2019: 無法解析的外部符號 [email protected],該符號在函數 _main 中被引用
error LNK2019: 無法解析的外部符號 [email protected],該符號在函數 _main 中被引用
error LNK2019: 無法解析的外部符號 [email protected],該符號在函數 _main 中被引用
error LNK2019: 無法解析的外部符號 [email protected],該符號在函數 _main 中被引用

解決方案:

原因如下,我使用的系統是win7x64,MySQL 64位的lib也是64位的介面。所以解決方案如下:
.項目->屬性->組態管理員
活動解決方案平台,下拉選建立,出現一個新的對號框,在鍵入選擇新平台中選擇X64

重新編譯  成功~~~

測試代碼2:

#include "stdafx.h"#include <WinSock2.h> /*socket通訊,系統標頭檔*/#include <windows.h>#include <stdio.h>#include "mysql.h"#pragma comment(lib, "libmysql.lib")int main(){    MYSQL my_connection;    char *server = "localhost";    char *user = "root";    char *password = "root";//“******”為你設定的密碼    char *database = "hyx";    mysql_init(&my_connection);    if(!mysql_real_connect(&my_connection, server, user, password, database, 0, NULL, 0))        printf("串連失敗:%sn", mysql_error(&my_connection));    else        printf("串連成功!");    getchar();    return 0;}

測試結果2:

 

問題:測試代碼1與測試代碼2隻有標黑的兩行不同,但是一個串連成功了,一個串連失敗了,這是什麼原因?

 

VS中快速鍵
F12 轉到定義處
ctrl+"-" 從定義處返回
F7 編譯
ctrl+F5  運行
F5 執行到下一個斷點
F10 調試(逐過程)
F11  調試(逐語句)

 



 

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.