Following is the complete code for the tutorial program described in the preceding sections.
#include <tchar.h> #include <stdio.h> #include "uliface.h" using namespace UltraLite; #define MAX_NAME_LEN 100 ULSqlca Tutca; static ul_char const * ConnectionParms = UL_TEXT( "UID=DBA;PWD=sql;" ) UL_TEXT( "DBF=C:\\temp\\ULCustomer.udb" ); ul_error_action UL_GENNED_FN_MOD MyErrorCallBack( SQLCA * Tutca, ul_void * user_data, ul_char * message_param ) { ul_error_action rc; (void) user_data; switch( Tutca->sqlcode ){ // The following error is used for flow control - don't report it here case SQLE_NOTFOUND: rc = UL_ERROR_ACTION_CONTINUE; break; default: if (Tutca->sqlcode >= 0) { // warning or success rc = UL_ERROR_ACTION_DEFAULT; } else { // negative is real error _tprintf( _TEXT( "Error %ld: %s\n" ), Tutca->sqlcode, message_param ); rc = UL_ERROR_ACTION_CANCEL; } break; } return rc; } Connection * open_conn( DatabaseManager * dm ) { Connection * conn = dm->OpenConnection( Tutca, ConnectionParms ); if( conn == UL_NULL ) { _tprintf( _TEXT("Unable to open existing database.\n") ); } return conn; } // Open table, insert 1 row if table is currently empty bool do_insert( Connection * conn ) { Table * table = conn->OpenTable( UL_TEXT("ULCustomer") ); if( table == UL_NULL ) { _tprintf( _TEXT("Table not found: ULCustomer\n") ); return false; } if( table->GetRowCount() == 0 ) { _tprintf( _TEXT("Inserting one row.\n") ); table->InsertBegin(); table->Set( UL_TEXT("cust_name"), UL_TEXT("New Customer") ); table->Insert(); conn->Commit(); } else { _tprintf( _TEXT("The table has %lu rows\n"), table->GetRowCount() ); } table->Release(); return true; }// Open table, display data from all rows bool do_select( Connection * conn ) { Table * table = conn->OpenTable( _TEXT("ULCustomer") ); if( table == UL_NULL ) { return false; } TableSchema * schema = table->GetSchema(); if( schema == UL_NULL ) { table->Release(); return false; } ul_column_num id_cid = schema->GetColumnID( UL_TEXT("cust_id") ); ul_column_num cname_cid = schema->GetColumnID( UL_TEXT("cust_name") ); schema->Release(); _tprintf( _TEXT("\n\nTable 'ULCustomer' row contents:\n") ); while( table->Next() ) { ul_char cname[ MAX_NAME_LEN ]; table->Get( cname_cid ).GetString( cname, MAX_NAME_LEN ); _tprintf( _TEXT("id=%d, name=%s \n"), (int)table->Get( id_cid ), cname ); } table->Release(); return true; } // sync database with MobiLink connection to reference database bool do_sync( Connection * conn ) { ul_synch_info info; ul_stream_error * se = &info.stream_error; ULEnableTcpipSynchronization( Tutca.GetCA() ); conn->InitSynchInfo( &info ); info.stream = ULSocketStream(); info.version = UL_TEXT( "custdb 11.0" ); info.user_name = UL_TEXT( "50" ); info.download_only = true; if( !conn->Synchronize( &info ) ) { _tprintf( _TEXT("Synchronization error \n" )); _tprintf( _TEXT(" stream_error_code is '%lu'\n"), se->stream_error_code ); _tprintf( _TEXT(" system_error_code is '%ld'\n"), se->system_error_code ); _tprintf( _TEXT(" error_string is '") ); _tprintf( _TEXT("%s"), se->error_string ); _tprintf( _TEXT("'\n") ); return false; } return true; } int main() { ul_char buffer[ MAX_NAME_LEN ]; Connection * conn; Tutca.Initialize(); ULRegisterErrorCallback( Tutca.GetCA(), MyErrorCallBack, UL_NULL, buffer, sizeof (buffer)); DatabaseManager * dm = ULInitDatabaseManager( Tutca ); if( dm == UL_NULL ){ // You may have mismatched UNICODE vs. ANSI runtimes. Tutca.Finalize(); return 1; } conn = open_conn( dm ); if( conn == UL_NULL ){ dm->Shutdown( Tutca ); Tutca.Finalize(); return 1; } do_insert (conn); do_sync (conn); do_select (conn); dm->Shutdown( Tutca ); Tutca.Finalize(); return 0; } |
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |