如題,今天給你們簡單演示一下VS2015如何鏈接mySQL數據庫。
首先呢,你們須要安裝vs2015和mySQL這兩個軟件,我還安裝了一個輔助軟件SQLyog - 32 bit(我師兄推薦我安裝的),還有個人mySQL版本是5.6 。廢話少講,開始了。
鏈接mySQL主要考慮兩個方面問題,添加mysql.h這個頭文件和libmysql.lib這個庫文件。
1.新建win32控制檯程序,添加源文件,將測試程序複製進去(個人實在網上隨便下載一個測試程序,是原來用來測試VC6.0鏈接數據庫的,不礙事,稍做修改都能用,若是你不想本身去網上找測試程序,那我就把測試程序粘上來吧,以下)
- <span style="font-size:12px;">#include <winsock.h>
- #include <iostream>
- #include <string>
- #include <mysql.h>
- using namespace std;
-
- #pragma comment(lib, "ws2_32.lib")
- #pragma comment(lib, "libmysql.lib")
-
- #define STEPBYSTEP
-
- int main() {
- cout << "****************************************" << endl;
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
- MYSQL mydata;
-
-
- if (0 == mysql_library_init(0, NULL, NULL)) {
- cout << "mysql_library_init() succeed" << endl;
- }
- else {
- cout << "mysql_library_init() failed" << endl;
- return -1;
- }
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
- if (NULL != mysql_init(&mydata)) {
- cout << "mysql_init() succeed" << endl;
- }
- else {
- cout << "mysql_init() failed" << endl;
- return -1;
- }
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
-
- if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {
- cout << "mysql_options() succeed" << endl;
- }
- else {
- cout << "mysql_options() failed" << endl;
- return -1;
- }
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
- if (NULL
- != mysql_real_connect(&mydata, "localhost", "root", "123\\", "test",3306, NULL, 0))
-
- {
- cout << "mysql_real_connect() succeed" << endl;
- }
- else {
- cout << "mysql_real_connect() failed" << endl;
- return -1;
- }
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
- string sqlstr;
-
-
- sqlstr = "CREATE TABLE IF NOT EXISTS user_info";
- sqlstr += "(";
- sqlstr +=
- "user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";
- sqlstr +=
- "user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";
- sqlstr +=
- "user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";
- sqlstr += ");";
- if (0 == mysql_query(&mydata, sqlstr.c_str())) {
- cout << "mysql_query() create table succeed" << endl;
- }
- else {
- cout << "mysql_query() create table failed" << endl;
- mysql_close(&mydata);
- return -1;
- }
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
- sqlstr =
- "INSERT INTO user_info(user_name) VALUES('公司名稱'),('一級部門'),('二級部門'),('開發小組'),('姓名');";
- if (0 == mysql_query(&mydata, sqlstr.c_str())) {
- cout << "mysql_query() insert data succeed" << endl;
- }
- else {
- cout << "mysql_query() insert data failed" << endl;
- mysql_close(&mydata);
- return -1;
- }
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
- sqlstr = "SELECT user_id,user_name,user_second_sum FROM user_info";
- MYSQL_RES *result = NULL;
- if (0 == mysql_query(&mydata, sqlstr.c_str())) {
- cout << "mysql_query() select data succeed" << endl;
-
-
- result = mysql_store_result(&mydata);
-
- int rowcount = mysql_num_rows(result);
- cout << "row count: " << rowcount << endl;
-
-
- unsigned int fieldcount = mysql_num_fields(result);
- MYSQL_FIELD *field = NULL;
- for (unsigned int i = 0; i < fieldcount; i++) {
- field = mysql_fetch_field_direct(result, i);
- cout << field->name << "\t\t";
- }
- cout << endl;
-
-
- MYSQL_ROW row = NULL;
- row = mysql_fetch_row(result);
- while (NULL != row) {
- for (int i = 0; i < fieldcount; i++) {
- cout << row[i] << "\t\t";
- }
- cout << endl;
- row = mysql_fetch_row(result);
- }
-
- }
- else {
- cout << "mysql_query() select data failed" << endl;
- mysql_close(&mydata);
- return -1;
- }
-
- #ifdef STEPBYSTEP
- system("pause");
- #endif
-
-
- sqlstr = "DROP TABLE user_info";
- if (0 == mysql_query(&mydata, sqlstr.c_str())) {
- cout << "mysql_query() drop table succeed" << endl;
- }
- else {
- cout << "mysql_query() drop table failed" << endl;
- mysql_close(&mydata);
- return -1;
- }
- mysql_free_result(result);
- mysql_close(&mydata);
- mysql_server_end();
-
- system("pause");
- return 0;
- }</span>
<span style="font-size:12px;">#include <winsock.h>
#include <iostream>
#include <string>
#include <mysql.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libmysql.lib")
//單步執行,不想單步執行就註釋掉
#define STEPBYSTEP
int main() {
cout << "****************************************" << endl;
#ifdef STEPBYSTEP
system("pause");
#endif
//必備的一個數據結構
MYSQL mydata;
//初始化數據庫
if (0 == mysql_library_init(0, NULL, NULL)) {
cout << "mysql_library_init() succeed" << endl;
}
else {
cout << "mysql_library_init() failed" << endl;
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//初始化數據結構
if (NULL != mysql_init(&mydata)) {
cout << "mysql_init() succeed" << endl;
}
else {
cout << "mysql_init() failed" << endl;
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//在鏈接數據庫以前,設置額外的鏈接選項
//能夠設置的選項不少,這裏設置字符集,不然沒法處理中文
if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {
cout << "mysql_options() succeed" << endl;
}
else {
cout << "mysql_options() failed" << endl;
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//鏈接數據庫
if (NULL
!= mysql_real_connect(&mydata, "localhost", "root", "123\\", "test",3306, NULL, 0))
//這裏的地址,用戶名,密碼,端口能夠根據本身本地的狀況更改
{
cout << "mysql_real_connect() succeed" << endl;
}
else {
cout << "mysql_real_connect() failed" << endl;
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//sql字符串
string sqlstr;
//建立一個表
sqlstr = "CREATE TABLE IF NOT EXISTS user_info";
sqlstr += "(";
sqlstr +=
"user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";
sqlstr +=
"user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";
sqlstr +=
"user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";
sqlstr += ");";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() create table succeed" << endl;
}
else {
cout << "mysql_query() create table failed" << endl;
mysql_close(&mydata);
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//向表中插入數據
sqlstr =
"INSERT INTO user_info(user_name) VALUES('公司名稱'),('一級部門'),('二級部門'),('開發小組'),('姓名');";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() insert data succeed" << endl;
}
else {
cout << "mysql_query() insert data failed" << endl;
mysql_close(&mydata);
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//顯示剛纔插入的數據
sqlstr = "SELECT user_id,user_name,user_second_sum FROM user_info";
MYSQL_RES *result = NULL;
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() select data succeed" << endl;
//一次性取得數據集
result = mysql_store_result(&mydata);
//取得並打印行數
int rowcount = mysql_num_rows(result);
cout << "row count: " << rowcount << endl;
//取得並打印各字段的名稱
unsigned int fieldcount = mysql_num_fields(result);
MYSQL_FIELD *field = NULL;
for (unsigned int i = 0; i < fieldcount; i++) {
field = mysql_fetch_field_direct(result, i);
cout << field->name << "\t\t";
}
cout << endl;
//打印各行
MYSQL_ROW row = NULL;
row = mysql_fetch_row(result);
while (NULL != row) {
for (int i = 0; i < fieldcount; i++) {
cout << row[i] << "\t\t";
}
cout << endl;
row = mysql_fetch_row(result);
}
}
else {
cout << "mysql_query() select data failed" << endl;
mysql_close(&mydata);
return -1;
}
#ifdef STEPBYSTEP
system("pause");
#endif
//刪除剛纔建的表
sqlstr = "DROP TABLE user_info";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
cout << "mysql_query() drop table succeed" << endl;
}
else {
cout << "mysql_query() drop table failed" << endl;
mysql_close(&mydata);
return -1;
}
mysql_free_result(result);
mysql_close(&mydata);
mysql_server_end();
system("pause");
return 0;
}</span>
2.添加頭文件和庫文件
1' 解決方案資源管理器-->右擊工程名-->屬性
2' 配置屬性-->C/C++-->常規-->附加包含目錄-->左擊空白處-->向下箭頭-->編輯
雙擊空白處
-->...-->選擇目錄-->選擇文件夾
這個目錄是擁有mysql.h的目錄,根據本身的安裝路徑不一樣,位置不一樣,通常在mysql的安裝目錄下
、
到了這一步,頭文件算是添加好了
下面添加庫文件,方法相似
仍是這個界面,C/C++下面一個選項
連接器-->常規-->附加庫目錄-->左擊空白處-->向下箭頭-->編輯-->雙擊空白處-->...-->選擇目錄-->選擇文件夾-->肯定。
庫文件主要是libmysql.lib文件所在文件夾
添加庫文件還需輸入
連接器-->輸入-->附加依賴項-->左擊右邊-->向下箭頭-->編輯-->輸入libmusql.lib-->肯定。
庫文件添加成功!
簡單修改測試程序
數據庫的地址,用戶名,密碼,端口能夠根據本身本地的狀況更改
若是對幾個變量意思不理解能夠右擊函數mysql_real_connect查看定義
提示:有朋友可能看到個人密碼是123\\感到是奇怪,看過我之前文章的同窗知道個人密碼是123\,可是你們都知道 \ 加上 不一樣的字母表示不一樣的意思,也就是轉義字符,C語言中 \\ 才表示 \
修改完成後編譯,成功
可是有的同窗會遇到libmysql.dll文件丟失的問題
解決方法
將lib目錄下的libmysql.dll拷貝到工程目錄下的debug文件下
到此結束了。 若有問題,歡迎交流,不正確的地方,歡迎指教