The following is the complete code for the tutorial program described in the preceding sections.
#include <tchar.h> #include <stdio.h> #include "ulcpp.h" #define MAX_NAME_LEN 100 static ul_char const * ConnectionParms = "UID=DBA;PWD=sql;DBF=c:\\tutorial\\cpp\\ULCustomer.udb"; ul_error_action UL_CALLBACK_FN MyErrorCallBack( const ULError * error, ul_void * user_data ) { ul_error_action rc; an_sql_code code = error->GetSQLCode(); (void) user_data; switch( code ){ // The following error is used for flow control - don't report it here case SQLE_NOTFOUND: rc = UL_ERROR_ACTION_CONTINUE; break; default: if (code >= 0) { // warning or success rc = UL_ERROR_ACTION_DEFAULT; } else { // negative is real error _tprintf( "Error %ld: %s\n", code, error->GetString() ); rc = UL_ERROR_ACTION_CANCEL; } break; } return rc; } static ULConnection * open_conn( void ) { ULConnection * conn = ULDatabaseManager::OpenConnection( ConnectionParms ); if( conn == UL_NULL ) { _tprintf( "Unable to open existing database.\n" ); } return conn; } static bool do_insert( ULConnection * conn ) { ULTable * table = conn->OpenTable( "ULCustomer" ); if( table == UL_NULL ) { _tprintf( "Table not found: ULCustomer\n" ); return false; } if( table->GetRowCount() == 0 ) { _tprintf( "Inserting one row.\n" ); table->InsertBegin(); table->SetString( "cust_name", "New Customer" ); table->Insert(); conn->Commit(); } else { _tprintf( "The table has %lu rows\n", table->GetRowCount() ); } table->Close(); return true; } static bool do_select( ULConnection * conn ) { ULTable * table = conn->OpenTable( "ULCustomer" ); if( table == UL_NULL ) { return false; } ULTableSchema * schema = table->GetTableSchema(); if( schema == UL_NULL ) { table->Close(); return false; } ul_column_num id_cid = schema->GetColumnID( "cust_id" ); ul_column_num cname_cid = schema->GetColumnID( "cust_name" ); schema->Close(); _tprintf( "\n\nTable 'ULCustomer' row contents:\n" ); while( table->Next() ) { ul_char cname[ MAX_NAME_LEN ]; table->GetString( cname_cid, cname, MAX_NAME_LEN ); _tprintf( "id=%d, name=%s \n", (int)table->GetInt(id_cid), cname ); } table->Close(); return true; } static bool do_sync( ULConnection * conn ) { ul_sync_info info; ul_stream_error * se = &info.stream_error; ULDatabaseManager::EnableTcpipSynchronization(); conn->InitSyncInfo( &info ); info.stream = "TCPIP"; info.version = "custdb 12.0"; info.user_name = "50"; info.download_only = true; if( !conn->Synchronize( &info ) ) { _tprintf( "Synchronization error \n" ); _tprintf( " stream_error_code is '%lu'\n", se->stream_error_code ); _tprintf( " system_error_code is '%ld'\n", se->system_error_code ); _tprintf( " error_string is '" ); _tprintf( "%s", se->error_string ); _tprintf( "'\n" ); return false; } return true; } int main() { ULConnection * conn; ULDatabaseManager::Init(); ULDatabaseManager::SetErrorCallback( MyErrorCallBack, NULL ); conn = open_conn(); if( conn == UL_NULL ){ ULDatabaseManager::Fini(); return 1; } // Main processing code goes here ... do_insert( conn ); do_select( conn ); do_sync( conn ); conn->Close(); ULDatabaseManager::Fini(); return 0; } |
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |