2009/03/04

MySQL Connector/C++ 1.0.3 Alpha Library

3월 3일자로 발표된 MySQL Connector/C++ 1.0.3 을 오래전에 소개한 MySQL Connector/C++ 1.0.1 Alpha 컴파일 하기에 따라 라이브러리를 만들었다.



컴파일러는 Visual C++ 2005를 사용하였고 MySQL은 5.0.77버전을 사용하여 Win32용과 Win64용을 만들어 보았다.



오랜만에 버전업이 되어 보다 안정적이 된 것 같다. 이번 버전은 컴파일러 환경에 따라 config.h 파일의 내용이 약간 바뀌므로 아래 첨부된 라이브러리를 사용하기 위해서는 함께 첨부된 include.7z 을 이용해야한다.













위 라이브러리들은 아래의 코드로 테스트를 수행하였다.



try
{
TRACE("Test Begin!!\n");

std::string strQuery;

// Driver 인스턴스 가져오기
sql::Driver* pDriver = sql::mysql::MySQL_Driver::Instance();
//sql::Driver* pDriver = sql::mysql::get_mysql_driver_instance();

if ( pDriver )
{
sql::Connection* pConn = pDriver->connect( "127.0.0.1", "root", "");
TRACE("Database Connected!!\n");

if ( pConn )
{
sql::PreparedStatement *pPreStmt;
sql::ResultSet *res = NULL;

////////////////////////////////////////
// Statement 생성
sql::Statement *pStmt = pConn->createStatement();

////////////////////////////////////////
// 데이터베이스 생성
pStmt->execute( "CREATE DATABASE IF NOT EXISTS TESTDB" );
TRACE("Database Created!!\n");

pStmt->execute( "USE TESTDB" );
TRACE("Select Database!!\n");

////////////////////////////////////////
// Table 생성
pStmt->execute( "DROP TABLE IF EXISTS TestTable" );
strQuery = "CREATE TABLE TestTable "
"( "
" name VARCHAR(30),"
" age INT,"
" big BIGINT"
")";
pStmt->execute( strQuery );
TRACE(" Table Created!!\n");

////////////////////////////////////////
// Insert
strQuery = "INSERT INTO TestTable ( name, age, big )"
" VALUES ( 'tester', 20, 0 );";
pStmt->execute( strQuery );
TRACE(" Insert!!\n");

////////////////////////////////////////
// Select
strQuery = "SELECT name, age FROM TestTable";
res = pStmt->executeQuery( strQuery );

TRACE(" Select Begin!!\n");
while( res->next() )
{
std::string name = res->getString( "name" );
int age = res->getInt( "age" );

CString strResult;
strResult.Format( _T(" name:%s, age:%d\n"),
CString( name.c_str()), age );
TRACE( strResult );
}
TRACE(" Select End!!\n");

delete res;

////////////////////////////////////////
// Update
strQuery = "UPDATE TestTable SET age = 25"
" WHERE name = 'tester';";
int rows = pStmt->executeUpdate( strQuery );
TRACE1(" Update (%d rows)!!\n", rows );

////////////////////////////////////////
// Select
strQuery = "SELECT name, age FROM TestTable ";
res = pStmt->executeQuery( strQuery );

TRACE(" Select Begin!!\n");
while( res->next() )
{
std::string name = res->getString( "name" );
int age = res->getInt( "age" );

CString strResult;
strResult.Format( _T(" name:%s, age:%d\n"),
CString( name.c_str()), age );
TRACE( strResult );
}
TRACE(" Select End!!\n");

delete res;

////////////////////////////////////////
// Delete
strQuery = "DELETE FROM TestTable";
rows = pStmt->executeUpdate( strQuery );
TRACE1(" Delete (%d rows)!!\n", rows );

////////////////////////////////////////
// PreprareStatement Insert
strQuery = "INSERT INTO TestTable ( name, age, big ) "
"VALUES ( ?, ?, ? );";
pPreStmt = pConn->prepareStatement( strQuery );

char name[100];

for ( int i = 0; i < 10; ++i )
{
sprintf_s( name, 100, "name%03d\0", i );

// 데이터 바인딩
pPreStmt->setString( 1, name );
pPreStmt->setInt( 2, 20 + i );
pPreStmt->setInt64( 3, 202012014 );

// Prepared Statement 수행
pPreStmt->executeUpdate();
TRACE(" PrepareStatement Insert!!\n");
}

// Statement 삭제
delete pPreStmt;

////////////////////////////////////////
// Select
strQuery = "SELECT name, age, big FROM TestTable ";
res = pStmt->executeQuery( strQuery );

TRACE(" Select Begin!!\n");
while( res->next() )
{
std::string name = res->getString( "name" );
int age = res->getInt( "age" );
int64_t big = res->getInt64( "big" );

CString strResult;
strResult.Format( _T(" name:%s, age:%d, big:%d\n"),
CString( name.c_str()), age, big );
TRACE( strResult );
}
TRACE(" Select End!!\n");

delete res;

////////////////////////////////////////
// PreprareStatment Update
strQuery = "UPDATE TestTable SET age = ? WHERE name = ?;";
pPreStmt = pConn->prepareStatement( strQuery );

for ( int i = 0; i < 10; ++i )
{
sprintf_s( name, 100, "name%03d\0", i );

// 데이터 바인딩
pPreStmt->setInt( 1, 30 + i );
pPreStmt->setString( 2, name );


// Prepared Statement 수행
pPreStmt->executeUpdate();
TRACE(" PrepareStatement Update!!\n");
}

// Statement 삭제
delete pPreStmt;

////////////////////////////////////////
// Select
strQuery = "SELECT name, age, big FROM TestTable ";
res = pStmt->executeQuery( strQuery );

TRACE(" Select Begin!!\n");
while( res->next() )
{
std::string name = res->getString( "name" );
int age = res->getInt( "age" );
INT64 big = res->getInt64( "big" );

CString strResult;
strResult.Format( _T(" name:%s, age:%d, big:%d\n"),
CString( name.c_str()), age, big );
TRACE( strResult );
}
TRACE(" Select End!!\n");

delete res;

// 데이터베이스 삭제
pStmt->execute( "DROP DATABASE IF EXISTS TESTDB" );
TRACE("Drop Database\n");

delete pStmt;
}
}

TRACE("Test Finished!!\n");
}
catch ( sql::SQLException &e )
{
// 예외처리 - 에러 메시지와 에러 코드를 가져올 수 있다.
int nErrorCode = e.getErrorCode();
std::string err = e.what();

TRACE1( "Exception : %s\n", err.c_str() );
}

Original Post : http://neodreamer-dev.tistory.com/262

No comments :

Post a Comment