我裝的是5.1版本,需要的頭文件有
成都創(chuàng)新互聯(lián)公司專注于烈山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供烈山營銷型網(wǎng)站建設(shè),烈山網(wǎng)站制作、烈山網(wǎng)頁設(shè)計(jì)、烈山網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造烈山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供烈山網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
把需要的文件添加進(jìn)去,然后再把 libMySQL.lib放到項(xiàng)目目錄里,文件在mysql安裝目錄 lib 下面.
#include "stdafx.h" #include <iostream> #include <winsock2.h> #include "mysql.h" //#pragma comment(lib, "ws2_32.lib") #pragma comment(lib,"libmysql.lib") using namespace std; int main(int argc, char* argv[]) { mysql_library_init(NULL,0,0); MYSQL mysql; mysql_init(&mysql); if(0==mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8"))//設(shè)置字符集 { cout << "設(shè)置字符集成功\n\n" <<endl; } if(!mysql_real_connect(&mysql,"localhost","root","kwgkwg","test",0,NULL,CLIENT_MULTI_STATEMENTS))//連接數(shù)據(jù)庫 { cout << "not connect mysql" << endl; }else { cout << "welcome to mysql\n\n\n"; } mysql_query(&mysql,"select * from demo1"); //執(zhí)行SQL語句 MYSQL_RES *result=mysql_store_result(&mysql); //獲取資源 int rowcount=mysql_num_rows(result); //獲取記錄數(shù) unsigned int fieldcount=mysql_num_fields(result); //獲取字段數(shù) //cout << rowcount << endl; MYSQL_FIELD *field=NULL; //字段 MYSQL_ROW row=NULL; //記錄 while(row=mysql_fetch_row(result)) { for(unsigned int i=0;i<fieldcount;i++) { field=mysql_fetch_field_direct(result,i); cout<<field->name<<":"<<row[i] <<"\n"; } } mysql_free_result(result); mysql_close(&mysql); mysql_server_end(); mysql_library_end(); return 0; }
C++訪問 (直接調(diào)用C-API)
#include <iostream> #include <windows.h> #include <mysql.h> #include <string> static const char host[32] = "localhost"; static const char user[32] = "test"; static const char passwd[32] = "passwd"; static const char db[32] = "test"; /** mysql> select * from t; +----+ | id | +----+ | 1 | +----+ 1 row in set (0.00 sec) mysql> delimiter // mysql> create procedure get_t(in t1 int) -> begin -> select id from t where id=t1; -> end -> // Query OK, 0 rows affected (0.05 sec) mysql> call get_t(1); -> // +----+ | id | +----+ | 1 | +----+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) */ void test_more_results(MYSQL* h) { char str[512] = "insert into test_num values(101);insert into test_num values(122);commit;"; int r = mysql_real_query(h, str, strlen(str)); if (r) { const char * error = mysql_error(h); std::cout<<"*** Connection Error " << error << std::endl; } do { MYSQL_RES* res = mysql_store_result(h); mysql_free_result(res); } while ( (0 == mysql_next_result(h)) ); } void test_proc_stmt(MYSQL* h) { MYSQL* mysql_ = h; MYSQL_BIND bind; MYSQL_BIND obind[1]; // test_more_results(mysql_); MYSQL_STMT *hStmt = mysql_stmt_init(mysql_); my_bool true_value= 1; mysql_stmt_attr_set(hStmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*) &true_value); char sql[] = "call get_t(?)"; //char sql[] = "select id from t where id=?"; if (mysql_stmt_prepare(hStmt, sql, strlen(sql))) { std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; mysql_stmt_reset(hStmt); if (mysql_stmt_prepare(hStmt, sql, strlen(sql))) { std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; mysql_close(mysql_); exit( -1); } } int id = 1; unsigned long id_len = 0; memset(&bind, 0, sizeof(bind)); bind.buffer_type = FIELD_TYPE_LONG; bind.buffer = (void*)&id; bind.is_unsigned = true; bind.length = &id_len; // bind[0].buffer_length = sizeof(id); // bind[0].is_null = 0; if (mysql_stmt_bind_param(hStmt,(MYSQL_BIND*)(&bind)) != 0) { std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; mysql_close(mysql_); exit( -1); } if (mysql_stmt_execute(hStmt) != 0) { std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; mysql_close(mysql_); exit( -1); } int t2; memset(obind, 0, sizeof(obind)); obind[0].buffer_type= MYSQL_TYPE_LONG; obind[0].buffer= (char *)&t2; obind[0].buffer_length = sizeof(t2); if (mysql_stmt_bind_result(hStmt, (MYSQL_BIND*)&obind[0]) != 0) { std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; mysql_close(mysql_); exit( -1); } if ( mysql_stmt_store_result(hStmt) != 0 ) { std::cout<<__LINE__<<": stmt prepare error: "<< (mysql_stmt_error(hStmt))<<std::endl; mysql_close(mysql_); exit( -1); } int rows = mysql_stmt_num_rows(hStmt); for (int i=0; i<rows; i++) { if (mysql_stmt_fetch(hStmt) == 0) { std::cout<<"id = "<<t2<<std::endl; } } mysql_stmt_free_result(hStmt); mysql_stmt_close(hStmt); } // // Just for demo only. // int main() { MYSQL* mysql_ = NULL; MYSQL_RES* result_ = NULL; MYSQL_ROW row_; mysql_ = mysql_init(mysql_); // if (mysql_real_connect(mysql_, host, user, passwd, db, 3306, NULL, CLIENT_MULTI_STATEMENTS) == NULL) if (mysql_real_connect(mysql_, host, user, passwd, db, 3306, NULL, CLIENT_MULTI_STATEMENTS) == NULL) { const char * error = mysql_error(mysql_); std::cout<<"*** Connection Error " << error << std::endl; return -1; } mysql_autocommit(mysql_, false); std::string encodeStr = "set names 'gbk'"; mysql_real_query(mysql_, encodeStr.c_str(), encodeStr.size()); /* const char* tmpTableName = "t"; // assume you are querying the table 't' char str[512]; int cnt = 0; sprintf(str,"select count(*) as cnt from %s", tmpTableName); mysql_real_query(mysql_, str, strlen(str)); result_ = mysql_store_result(mysql_); while (row_ = mysql_fetch_row(result_)) { // get the field value if (row_[0]) { std::cout<<"count = "<<row_[0]<<std::endl; // convert it into int cnt = atoi(row_[0]); std::cout<<"cnt value = "<<row_[0]<<std::endl; } } mysql_free_result(result_); test_more_results(); */ test_proc_stmt(mysql_); do { MYSQL_RES* res = mysql_store_result(mysql_); mysql_free_result(res); } while ( (0 == mysql_next_result(mysql_)) ); test_proc_stmt(mysql_); mysql_close(mysql_); return 0; }
這樣就差不多了,大家可以根據(jù)需要選擇。
本文標(biāo)題:C++連接mysql的方法(直接調(diào)用C-API)
文章鏈接:http://chinadenli.net/article32/gohepc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、品牌網(wǎng)站制作、App開發(fā)、企業(yè)建站、商城網(wǎng)站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)