centos linux mysql++(mysql c++操作类) 使用数据库实例

test.cpp

/*
#include <mysql++.h>

int main()
{
    mysqlpp::String greeting("Hello, world!");
    std::cout << greeting << std::endl;
    return 0;
}
*/

//#include <winsock2.h>  //
#include "stdio.h"
#include "mysql++.h"  //
#include <string>
#include <iostream>
using namespace mysqlpp;
using namespace std;

//const DWORD SpaceTime = 20 * 60 * 1000; // 20minutes
const int LEN_NAME = 8;
const char CCH[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //

//
char* rand_password( char* str, int len )
{
 int i = 0, n = 0;
 int nLength = strlen( CCH );
 for ( i = 0, n = 0; i < LEN_NAME && n < nLength; ++i, ++n )
 {
  int x = rand() % ( nLength - 1 );
  str[i] = CCH[x];
 }

 str[ i + 1 ] = '\0';
 return str;
}

int main()//int argc, _TCHAR* argv[]
{
 std::cout << "VPN_Modify_MySql_User_Password Tool" << std::endl;
 std::cout << "Copyright (C) eliteYang" << std::endl;
 std::cout << "http://www.cppfans.org" << std::endl;

 mysqlpp::Connection _vpnConn;
 //
 _vpnConn.set_option( new mysqlpp::SetCharsetNameOption( "utf8" ) );

 string dbIp="192.168.16.204";
 string dbUserName="launchcloud";
 string dbPwd="Launch-2013-Cloud";
 string dbName="launchcloud";

 


 bool bConnect = _vpnConn.connect( dbName.c_str(), dbIp.c_str(), dbUserName.c_str(), dbPwd.c_str(), 3366 );
 if ( !bConnect )
 {
  cout << "VPN Connect mysql failed! Error: " << _vpnConn.error() << endl;
  system( "Pause" );
  return -1;
 }
 else
 {
  cout << "VPN Connect mysql success!" << endl;
 }

 //DWORD timeSlot = timeGetTime();
 char szChar[ 256 ] = { 0 };
 string strName = "yandong";


 sprintf( szChar, "select * from classuser where username='%s'", strName.c_str() );
 while ( true )
 {
  //if ( timeGetTime() - timeSlot < SpaceTime )
  //{ continue; }
  try
  {
   mysqlpp::Query _query = _vpnConn.query( szChar );
   mysqlpp::StoreQueryResult _result = _query.store();
   if ( _result.num_rows() != 1 )
   {
    cout << "UserName[123] repeat, please check" << endl;
    //timeSlot = timeGetTime();
    continue;
   }

   string strUserName = _result[0][0].c_str();
   string strUserPassword = _result[0][1].c_str();
   cout << "CurentInfo UserName[ " << strUserName.c_str() << " ] Password[ " << strUserPassword.c_str() << " ]" << endl;

   char strTemp[ LEN_NAME + 1 ] = { 0 };
   strUserPassword = rand_password( strTemp, LEN_NAME );

   char szTemp[ 256 ] = { 0 };
   sprintf( szTemp, "UPDATE classuser SET passwd = '%s' WHERE username = '%s';", strUserPassword.c_str(), strUserName.c_str() );
   _query << szTemp << endl;
   _query.execute();
   cout << "ModifyUserInfo UserName[ " << strUserName.c_str() << " ] Password[ " << strUserPassword.c_str() << " ]" << endl;
  }
  catch (const mysqlpp::BadQuery& er)
  {
   // Handle any query errors
   cerr << "Query error: " << er.what() << endl;
   return -1;
  }
  catch (const mysqlpp::BadConversion& er)
  {
   // Handle bad conversions
   cerr << "Conversion error: " << er.what() << endl <<
    "\tretrieved data size: " << er.retrieved <<
    ", actual size: " << er.actual_size << endl;
   return -1;
  }
  catch (const mysqlpp::Exception& er)
  {
   // Catch-all for any other MySQL++ exceptions
   cerr << "Error: " << er.what() << endl;
   return -1;
  }
 }

 _vpnConn.disconnect();
 system( "Pause" );

 return 0;
}

 

///makefile

 

CXXFLAGS := -I/usr/include/mysql -I/usr/local/include/mysql++
LDFLAGS := -L/usr/local/lib
LDLIBS := -lmysqlpp -lmysqlclient
EXECUTABLE := hello

all: $(EXECUTABLE)

clean:
 rm -f $(EXECUTABLE) *.o


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值