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