ITPub博客

首页 > Linux操作系统 > Linux操作系统 > c++ 连接mysql实例

c++ 连接mysql实例

原创 Linux操作系统 作者:dimen007 时间:2010-08-19 15:30:57 0 删除 编辑


#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/,如需转载,请注明出处,否则将追究法律责任。

下一篇: linux heartbeat mysql
请登录后发表评论 登录
全部评论

注册时间:2010-06-17

  • 博文量
    11
  • 访问量
    20948