Eclipse中C++連接mysql數(shù)據(jù)庫
本文為大家分享了Eclipse中C++連接mysql數(shù)據(jù)庫的具體代碼,供大家參考,具體內(nèi)容如下
MinGW的配置在C連mysql里已經(jīng)提到,這里只說C++跟C配置不同的地方。
首先覺得C++比C方便多了,不用生成.a連接文件,直接使用.lib文件即可
導(dǎo)入.h文件所在目錄到C++ Compiler中的includes

添加lib文件搜索

代碼:
#include <winsock.h>
#include <iostream>
#include <string>
#include <mysql.h>
using namespace std;
int main() {
MYSQL mydata;
//初始化數(shù)據(jù)庫
mysql_library_init(0, NULL, NULL);
mysql_init(&mydata);
mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk");
//連接數(shù)據(jù)庫
if (NULL != mysql_real_connect(&mydata, "localhost", "root", "111111", "mysql", 3306, NULL, 0)) {
cout << "mysql_real_connect() succeed" << endl;
} else {
cout << "mysql_real_connect() failed" << endl;
return -1;
}
string sqlstr = "SELECT * FROM ME_MENU";
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 (unsigned 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;
}
mysql_free_result(result);
mysql_close(&mydata);
mysql_server_end();
system("pause");
return 0;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言深入分析數(shù)組指針和指針數(shù)組的應(yīng)用
在C語言和C++等語言中,數(shù)組元素全為指針變量的數(shù)組稱為指針數(shù)組,指針數(shù)組中的元素都必須具有相同的存儲類型、指向相同數(shù)據(jù)類型的指針變量。指針數(shù)組比較適合用來指向若干個字符串,使字符串處理更加方便、靈活2022-04-04
聊聊Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題
這篇文章主要介紹了Qt+OpenCV聯(lián)合開發(fā)之圖像的創(chuàng)建與賦值問題,給大家介紹了圖像的克隆及拷貝問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
從c++標(biāo)準(zhǔn)庫指針萃取器談一下traits技法(推薦)
本篇文章基于gcc中標(biāo)準(zhǔn)庫源碼剖析一下標(biāo)準(zhǔn)庫中的模板類pointer_traits,并且以此為例理解一下traits技法,對c++ traits技法源碼分析感興趣的朋友跟隨小編一起看看吧2021-07-07
C++流程控制中用于跳轉(zhuǎn)的return和goto語句學(xué)習(xí)教程
這篇文章主要介紹了C++流程控制中用于跳轉(zhuǎn)的return和goto語句學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01

