2010/12/26

Firebird C API 간단 사용기

C API를 이용한 Firebird embedded 를 간단하게 사용하는 예이다. 질의문의 결과난 파라메터를 이용한 방법은 XSQLDA(eXtended SQL Descriptor Area)를 이해한 후에 정리를 해야 할 것 같다.





ClearResult();
AppendResult( _T("Try to attach database!!") );
isc_db_handle db = 0L; /* database handle */
isc_tr_handle trans = NULL; /* transaction handle */
isc_stmt_handle stmt = NULL; /* statement handle */

char szDBName[ MAX_PATH ] = "E:\TEST.FDB";
ISC_STATUS_ARRAY status;
ISC_LONG sqlcode;

// Database Parameter Block (DPB) 준비
std::string strDPB;

strDPB.append( 1, isc_dpb_version1 ); // DPB 버전
strDPB.append( 1, isc_dpb_user_name ); // User Account
strDPB.append( 1, (char)strlen("sysdba") );
strDPB.append( "sysdba" );
strDPB.append( 1, isc_dpb_password ); // Password
strDPB.append( 1, (char)strlen("masterkey") );
strDPB.append( "masterkey" );

// Database connect
if ( isc_attach_database( status, 0, szDBName, &db, (short)strDPB.length(), strDPB.c_str() ) )
{
AppendResult( _T("Fail to attach database!!") );

return;
}

// Start Transaction
isc_start_transaction( status, &trans, 1, &db, 0, NULL );

// Execute query
char szQuery[1024];

// Create Table
sprintf_s( szQuery, 1024,
"CREATE TABLE employee ("
" name VARCHAR(20) "
", birth INTEGER"
", enter TIMESTAMP"
")");

isc_dsql_execute_immediate( status, &db, &trans, strlen(szQuery), szQuery, 1, NULL );
isc_commit_transaction( status, &trans );

if ( status[0] == 1 && status[ 1 ] > 0 )
{
// Error occurred
sqlcode = isc_sqlcode( status );

char szMsg[512];
isc_sql_interprete( (short)sqlcode, szMsg, 512 );

CString strMsg;
strMsg.Format( _T("Fail to Create Table : %d - %s"), sqlcode, CString( szMsg ) );
AppendResult( strMsg );
}
else
{
AppendResult( _T("Table Creaded.") );
}

// Insert Data
isc_start_transaction( status, &trans, 1, &db, 0, NULL );
AppendResult( _T("Insertion!!") );
for ( int i = 0; i < 5; ++i )
{
sprintf_s( szQuery, 1024,
"INSERT INTO employee ( name, birth, enter ) VALUES ( 'name%d', %d, CURRENT_TIMESTAMP )"
, i, 20 + i
);

isc_dsql_execute_immediate( status, &db, &trans, strlen(szQuery), szQuery, 1, NULL );

if ( status[0] == 1 && status[ 1 ] > 0 )
{
// Error occurred
sqlcode = isc_sqlcode( status );

char szMsg[512];
isc_sql_interprete( (short)sqlcode, szMsg, 512 );

CString strMsg;
strMsg.Format( _T("Fail to Insert : %d - %s"), sqlcode, CString( szMsg ) );
AppendResult( strMsg );
}
else
{
AppendResult( _T("1 Row Inserted.") );
}
}

// Update Data
// Statement allocation
isc_dsql_allocate_statement( status, &db, &stmt );

AppendResult( _T("Update!!") );
sprintf_s( szQuery, 1024, "UPDATE employee SET birth = birth + 2 WHERE birth > 22");

isc_dsql_prepare( status, &trans, &stmt, 0, szQuery, 0, NULL );

isc_dsql_execute( status, &trans, &stmt, 0, NULL );
isc_commit_transaction( status, &trans );

if ( status[ 0 ] == 1 && status[ 1 ] > 0 )
{
// Error occurred
sqlcode = isc_sqlcode( status );

char szMsg[512];
isc_sql_interprete( (short)sqlcode, szMsg, 512 );

CString strMsg;
strMsg.Format( _T("Fail to Update : %d - %s"), sqlcode, CString( szMsg ) );
AppendResult( strMsg );
}
else
{
AppendResult( _T("Table Updated") );
}

isc_dsql_free_statement( status, &stmt, DSQL_close );

// Delete Table
AppendResult( _T("Drop Table!!") );
sprintf_s( szQuery, 1024, "DROP TABLE employee");

isc_dsql_execute_immediate( status, &db, &trans, strlen(szQuery), szQuery, 1, NULL );

// Commit Transaction
isc_commit_transaction( status, &trans );

// Database disconnect
isc_detach_database( status, &db );

AppendResult( _T("Database detached!!") );
AppendResult( _T("Finished!!") );
<

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

No comments :

Post a Comment