c++ 連線mysql例項

dimen007發表於2010-08-19


#include
#include
#include
#include "C:\Program Files\MySQL\MySQL Server 5.1\include\mysql.h"
#define HOSTADDRESS "127.0.0.1"
#define USER "test0"
#define PWD "test"
#define DBNAME "log"
using namespace std;

#pragma comment(lib ,"C:/Program Files/MySQL/MySQL Server 5.1/lib/opt/libmysql.lib")

void _SystemTime()
{
 SYSTEMTIME systm ;
 GetLocalTime(&systm);
 cout << systm.wYear << "-" << systm.wMonth << "-"<< systm.wDay << " " << systm.wHour << ":" << systm.wMinute << ":" << systm.wSecond << " " << systm.wMilliseconds ;
}

class cMysqlConnect
{
private:
 MYSQL * m_connmysql;
 string m_err;
public:
 cMysqlConnect(const std::string &host ,const std::string &user ,const std::string &password ,const std::string &dbname ,unsigned int port);
 ~cMysqlConnect();
 MYSQL * GetConnect();
 void disconnect(MYSQL *m_connmysql);
 const std::string What();
};

cMysqlConnect::cMysqlConnect(const std::string &host ,const std::string &user ,const std::string &password ,const std::string &dbname ,unsigned int port = MYSQL_PORT):m_connmysql(NULL)
{
 if((m_connmysql = mysql_init(m_connmysql)) == NULL)
  return ;
 if(NULL == mysql_real_connect(m_connmysql ,host.c_str() ,user.c_str() ,password.c_str() ,dbname.c_str() ,port ,NULL ,0))
 {
  m_err = mysql_error(m_connmysql);
  return ;
 }
}

void cMysqlConnect::disconnect(MYSQL *m_connmysql)
{
 mysql_close(m_connmysql);
}

cMysqlConnect::~cMysqlConnect()
{
 if(m_connmysql)
 {
  mysql_close(m_connmysql) ;
  m_connmysql = NULL ;
 }
}

 

MYSQL * cMysqlConnect::GetConnect()
{
 if(m_connmysql == NULL)
 {
  cout << "connect fail\n" ;
 }
 return m_connmysql ;
}

const std::string cMysqlConnect::What()
{
 return mysql_error(m_connmysql) ;
}

int main(int argc ,char * argv[])
{
 cMysqlConnect *myconn = NULL ;
 int i = 0 ;

 //while(i<5000){
 //連線資料庫
 if(argc == 1)
 {
  myconn = new cMysqlConnect(HOSTADDRESS ,USER ,PWD ,DBNAME ,MYSQL_PORT) ;
  if(myconn == NULL)
  {
   std::cout << "allocate memory exception \n" ;
   return -1 ;
  }
  if(myconn->GetConnect() == NULL)
  {
   std::cout << "connect database fail" << endl <What().c_str() << endl;
   return -1 ;
  }

 }
 else if(argc == 6)
 {
  myconn = new cMysqlConnect(argv[1] ,argv[2] ,argv[3] ,argv[4] ,atoi(argv[5])) ;
  if(myconn == NULL)
  {
   std::cout << "allocate memory exception \n" ;
   return -1;
  }
  if(myconn->GetConnect() == NULL)
  {
   std::cout << "connect database fail" << endl <What().c_str() << endl;
   return -1 ;
  }
 }
 else
 {
  std::cout << "run parameter error\n" ;
  return -1;
 }

 MYSQL *mydata = myconn->GetConnect() ;

 //mysql_close(mydata) ;
 //i++ ;
 //}

 //std::string s_sql = "";
 
 //刪除資料表
 std::string s_sql = "drop table IF EXISTS cc" ;
 if(mysql_query(mydata ,s_sql.c_str()) != 0)
  std::cout << "drop table fail" << endl << mysql_error(mydata) << endl ;
 else
  cout << "drop table success" << endl ;

 //建立資料表
 s_sql = "CREATE TABLE cc(" ;
 s_sql += "id INT NOT NULL AUTO_INCREMENT ," ;
 s_sql += "c1 varchar(30) DEFAULT NULL ," ;
 s_sql += "cre_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ," ;
 s_sql += "PRIMARY KEY(id) ,KEY(c1))ENGINE = MYISAM " ;
 //PARTITION BY RANGE (id)(PARTITION p0 VALUES LESS THAN (50000) ENGINE = MYISAM ,PARTITION p1 VALUES LESS THAN (100000) ,PARTITION p2 VALUES LESS THAN (150000) ,PARTITION p3 VALUES LESS THAN (200000) ,PARTITION p4 VALUES LESS THAN (250000) ,PARTITION p5 VALUES LESS THAN (300000) ,PARTITION p6 VALUES LESS THAN (350000) ,PARTITION p7 VALUES LESS THAN (400000) ,PARTITION p8 VALUES LESS THAN (450000) ,PARTITION p9 VALUES LESS THAN (500000) ,PARTITION p10 VALUES LESS THAN MAXVALUE)

 if(mysql_query(mydata ,s_sql.c_str()) != 0)
 {
  cout << "create table fail :" << s_sql << mysql_error(mydata) <  system("pause") ;
  return -1 ;
 }
 else
  cout << "create table success\n" ;
 i = mysql_dump_debug_info(mydata) ;
 i = 0 ;


 //插入資料


 //針對事務性的儲存引擎。設定為不自動提交,可以有助於提高插入的速度,減少I/O
 mysql_autocommit(mydata ,1) ;
 _SystemTime();
 cout << endl;
 while(i < 500)
 {
  char s_str[100];
  sprintf(s_str,"%d",i);
  s_sql = s_str;
  s_sql = "INSERT INTO totalcharlog(amount ,logtime ,flag) VALUES(11,now() ,'1')";
  if(mysql_query(mydata ,s_sql.c_str()) != 0)
  {
   cout << "insert fail ,error msg :" << myconn->What().c_str() << s_sql <   
   myconn->disconnect(mydata);
   myconn = new cMysqlConnect(HOSTADDRESS ,USER ,PWD ,DBNAME ,MYSQL_PORT) ;
   mydata = myconn->GetConnect() ;
   return -1 ;
  }
  Sleep(10) ;
  i++ ;
 }
 _SystemTime();
 /*s_sql = "INSERT INTO cb VALUES(2 ,3)" ;
 if(mysql_query(mydata ,s_sql.c_str()) != 0)
 {
  cout << "Insert cb fail ,err msg :" << myconn->What().c_str() << endl ;
 }*/

 cout << "Autoincrement value: " << mysql_insert_id(mydata) << endl ;
 
 mysql_commit(mydata) ;
 
 //查詢資料表
 s_sql = "SELECT SQL_NO_CACHE * FROM cb" ;
 /*while(i < 50000)
 {*/
 if(mysql_query(mydata ,s_sql.c_str()) != 0)
 {
  cout << "exec fail\n" ;
  //return -1;
 }
 
// mysql_close(mydata) ;

 MYSQL_RES *result = mysql_store_result(mydata) ;
 //取得總記錄數
 long rowcount = mysql_num_rows(result) ;
 cout << "exec sql:" << s_sql.c_str() << ",row count: " <

 MYSQL_FIELD *fields = NULL ;
 //取得各欄位名
 for(int i = 0 ;fields = mysql_fetch_field(result) ;++i)
  cout << fields->flags << "\t\t" ;
 cout << endl;

 //一次讀取每條記錄
 MYSQL_ROW currrow = NULL ;

 //從記錄的某行開始讀取直到最後一行,相當於limit,作用是修改result的結果集
 mysql_data_seek(result ,1) ;

 while((currrow = mysql_fetch_row(result)) != NULL)
 {
  //讀該行的記錄
  for(int i = 0 ; i < mysql_num_fields(result) ;++i)
   cout << (currrow[i]?currrow[i]:"NULL") << "\t" ;
  cout << endl ;
 }
 mysql_free_result(result);
 /*i++ ;
 }*/
 
 //獲取Mysql客戶端資訊
 cout << mysql_get_server_info(mydata)<< endl ;
 
 system("pause") ;
 return 1 ;
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12309491/viewspace-671400/,如需轉載,請註明出處,否則將追究法律責任。

相關文章