#ifndef Mysql_manager_h#defineMysql_manager_h#include<Winsock2.h>#include"mysql.h"#include<string>#include<vector>#include<map>using namespacestd;classmysql_db{ Public: mysql_db (); ~mysql_db (); Public: /*Mysql_open () return:1 OK-1 failed*/ intMysql_open (Const Char* Host,Const Char* User,Const Char* Password,Const Char* Database, unsignedintport); /*Mysql_noresult_query (); Non-SELECT statement query return >0 succeeded, number of rows affected-1 failed*/ intMysql_noresult_query (Const Char*SQL); /*Mysql_select_query (); Query with result set return >0 OK returns result set number-1 failure map_results first = line second = values*/ intMysql_select_query (Const Char* SQL, map<int,vector<string>> &map_results); /*Mysql_select_singleline_query (); There is only one piece of data, or a query with only one field N bar. Call the vector directly to*/ intMysql_select_singleline_query (Const Char* SQL, vector<string> &v_results); /*Mysql_lasterror (); Returns the most recent error message*/ stringmysql_lasterror ();Private: MYSQL Sqlcon; Mysql_res*M_presult; Mysql_row M_row;};#endif
#include"stdafx.h"#include"managesql.h"mysql_db::mysql_db () {Mysql_init (&sqlcon);//MySQL initialization}mysql_db::~mysql_db () {mysql_close (&sqlcon);//Close Connection}intMysql_db::mysql_open (Const Char* Host,Const Char* User,Const Char* Password,Const Char* Database, unsignedintPort) { CharNvalue =1; Mysql_options (&sqlcon, Mysql_opt_reconnect, (Char*) &nvalue);//Wire Break Auto-re-connectMysql_options (&sqlcon, Mysql_set_charset_name,"GBK");//set name GBK Chinese garbled problem if(!mysql_real_connect (&sqlcon, host, user, password, database, port, NULL,0)){ return-1; } return 1;}intMysql_db::mysql_noresult_query (Const Char*SQL) { if(mysql_query (&sqlcon, sql)! =0){ return-1; } return(int) Mysql_affected_rows (&sqlcon);}intMysql_db::mysql_select_query (Const Char* SQL, map<int, vector<string>> &map_results) { if(mysql_query (&sqlcon, sql)! =0){ return-1; } if(! (M_presult = Mysql_use_result (&Sqlcon))) { return-1; } inti =0; intCount =Mysql_num_fields (M_presult); while(M_row =mysql_fetch_row (M_presult)) {Vector<string>Vval; for(intj =0; J < Count; J + +) {vval.push_back (m_row[j]); } map_results[i++] =Vval; } mysql_free_result (M_presult); returni;}intMysql_db::mysql_select_singleline_query (Const Char* SQL, vector<string> &v_results) { if(mysql_query (&sqlcon, sql)! =0){ return-1; } if(! (M_presult = Mysql_use_result (&Sqlcon))) { return-1; } inti =0; intCount =Mysql_num_fields (M_presult); while(M_row =mysql_fetch_row (M_presult)) { for(intj =0; J < Count; J + +) {v_results.push_back (m_row[j]); }} mysql_free_result (M_presult); returni;}stringMysql_db::mysql_lasterror () {returnMysql_error (&sqlcon);}
//ConsoleApplication2.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include<stdio.h>#include<stdlib.h>#include<iostream>#include<string>#include"ManageSQL.h"#include<vld.h>using namespacestd;intMain () {mysql_db mydb; //Open: Connect if(-1= = Mydb.mysql_open ("Localhost","Root","123456","Electronicpolice", Mysql_port)) cout<< Mydb.mysql_lasterror () <<Endl; //Create a library stringsql ="CREATE database if not exists database_test"; if( -1==Mydb.mysql_noresult_query (Sql.c_str ())) cout<< Mydb.mysql_lasterror () <<Endl; //Use librarysql ="Use database_test"; if( -1==Mydb.mysql_noresult_query (Sql.c_str ())) cout<< Mydb.mysql_lasterror () <<Endl; //Create a tablesql ="CREATE table if not EXISTS table_test (ID int. NOT NULL auto_increment, name varchar (a) NOT NULL, age int. NOT NULL, PRI Mary Key (ID))"; if( -1==Mydb.mysql_noresult_query (Sql.c_str ())) cout<< Mydb.mysql_lasterror () <<Endl; //Insert 2 piece of datasql ="INSERT INTO Table_test (name,age) VALUES (' Newsoul ', ' + ')"; if( -1==Mydb.mysql_noresult_query (Sql.c_str ())) cout<< Mydb.mysql_lasterror () <<Endl; SQL="INSERT INTO Table_test (name,age) VALUES (' Baidu ', ' + ')"; if( -1==Mydb.mysql_noresult_query (Sql.c_str ())) cout<< Mydb.mysql_lasterror () <<Endl; //Mysql_select_singleline_query Demosql ="SELECT * from Table_test where id=1"; Vector<string>Vresults; if(-1==Mydb.mysql_select_singleline_query (Sql.c_str (), vresults)) cout<< Mydb.mysql_lasterror () <<Endl; //traversing result setscout <<"results of the SELECT * from Table_test where id=1:"<<Endl; for(vector<string>::iterator it = Vresults.begin (); It! = Vresults.end (); it++) cout<< (*it) <<Endl; cout<< Endl <<Endl; //Mysql_select_query Demosql ="SELECT * from Table_test"; inti =0; while(I++ < -) {Map<int,vector<string>>Map_res; if(-1==Mydb.mysql_select_query (Sql.c_str (), map_res)) cout<< Mydb.mysql_lasterror () <<Endl; //Traverse ...cout <<"results of the SELECT * from Table_test:"<<Endl; for(map<int,vector<string>>::iterator it = Map_res.begin (); It! = Map_res.end (); it++) {cout<<"Section"<< it->first+1<<"article:"; for(intI=0; I<it->second.size (); i++) {cout<< It->second[i] <<"\ t"; } cout<<Endl; }} system ("Pause"); return 0;}
C + + MySQL Package class