Professional Documents
Culture Documents
Code Warrior ODBCContact Sample
Code Warrior ODBCContact Sample
Requirements 2
Appendix A: odbcex.c 14
Appendix B: odbcex.h 21
Appendix C: ContactController.m 24
Appendix D: ContactController.h 28
Appendix E: example.h 29
Appendix G: environment.plist 32
Legal Notice 33
Contact Us . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1
Copyright © 2004 iAnywhere Solutions, Inc.
Overview
This tutorial helps you create a sample Cocoa application with Metrowerks
CodeWarrior on Mac OS X that queries the contact table within the Adaptive
Server Anywhere sample database (asademo.db). The application provides the
ability move through the contact table with a bi-directional cursor and save
changes to Street and Zip data. The end result should appear as follows:
The document does not attempt to assist you with coding with ODBC or with
coding in Objective-C, C, or C++; there are many resources available to assist you
with these topics on the web. Instead, the document assists you in building a
Code Warrior project by putting together the required components within the IDE.
Requirements
♦ SQL Anywhere Studio 9.0.1 for Mac OS X, or later
♦ Mac OS X 10.2.3 or later
♦ CodeWarrior 8.0 or later
2
Copyright © 2004 iAnywhere Solutions, Inc.
6. Click OK.
Once you have added the Adaptive Server Anywhere ODBC driver, you can
create an ODBC data source that will be used to connect to the database.
3
Copyright © 2004 iAnywhere Solutions, Inc.
2. In the ODBC Administrator, click the User DSN tab, and then click Add.
3. Choose the ASA 9.0 driver from the list of available drivers. Click OK.
4. Type ASATutorial in the Data Source Name field and add the following
connection parameters, as shown below.
4
Copyright © 2004 iAnywhere Solutions, Inc.
Keyword Value
UID dba
PWD sql
START dbeng9
DBF /Applications/SQLAnywhere9/System/asademo.db
ThreadManager ON
Driver /Applications/SQLAnywhere9/System/lib/libdbodbc9.-
bundle
In some cases, the Driver parameter may not appear in the keywords list once
it is entered. If you want to confirm that the Driver parameter is included in the
data source, you can check the list of parameters in the $
Home/Library/ODBC/odbc.ini file.
☞ For more information about connections, refer to the “Connection and
Communication Parameters” chapter of the Adaptive Server Anywhere
Database Administration Guide.
5. Click Add.
6. Press C OMMAND+Q to exit the OpenLink Driver Manager or the iODBC ODBC
Administrator.
Alternatively, you can add the information with a text editor. The ODBC
configuration files are located in Library/ODBC within your Home folder. There is
an odbcinst.ini file for driver information and an odbc.ini file for data source
information. Sample files can be found in “Appendix F: Sample odbc.ini and
odbcinst.ini” on page 31.
5
Copyright © 2004 iAnywhere Solutions, Inc.
2. On the Project tab, select Mac OS X Cocoa Stationary from the list, and type
contactsample as the name of the project in the Project Name field.
3. Click OK.
4. Choose Objective-C Application project stationary, which should be the
default.
5. Click OK.
The CodeWarrior IDE appears.
6
Copyright © 2004 iAnywhere Solutions, Inc.
7
Copyright © 2004 iAnywhere Solutions, Inc.
10. Close the odbcex.c file and save it in the project folder.
11. Choose File ➤ New.
The New dialog appears.
12. Select the File tab and click Text File. In the left pane, type odbcex.h in the
Name field and type the project folder in the Location field.
13. When prompted with an empty text editor, copy the code from “Appendix B:
odbcex.h” on page 21 into the text editor.
14. Close the odbcex.h file and save it in the project folder.
15. Choose File ➤ New.
The New dialog appears.
16. Select the File tab and click Text File. In the left pane, type
ContactController.m in the Name field and type the project folder in the
Location field.
17. When prompted with an empty text editor, copy the code from “Appendix C:
ContactController.m” on page 24 into the text editor.
18. Close the ContactController.m file and save it in the project folder. If you get a
warning about a file mapping, ignore it.
19. Choose File ➤ New.
The New dialog appears.
20. Select the File tab and click Text File. In the left pane, type
ContactController.h in the Name field and type the project folder in the
Location field.
21. When prompted with an empty text editor, copy the code from “Appendix D:
ContactController.h” on page 28 into the text editor.
22. Close the ContactController.h file and save it in the project folder.
23. Choose File ➤ New.
The New dialog appears.
24. Select the File tab and click Text File. In the left pane, type example.h in the
Name field and type the project folder in the Location field.
25. When prompted with an empty text editor, copy the code from “Appendix E:
example.h” on page 29 into the text editor.
26. Close the example.h file and save it in the project folder.
8
Copyright © 2004 iAnywhere Solutions, Inc.
If you are using CodeWarrior version 9, add the line #include "prefix.h" to
the beginning of the prefix file, as follows:
9
Copyright © 2004 iAnywhere Solutions, Inc.
4. After removing focus from this pane, you may be prompted to build/make the
project. Click OK to complete the project.
10
Copyright © 2004 iAnywhere Solutions, Inc.
11
Copyright © 2004 iAnywhere Solutions, Inc.
12
Copyright © 2004 iAnywhere Solutions, Inc.
13
Copyright © 2004 iAnywhere Solutions, Inc.
Appendix A: odbcex.c
/*
* odbcex.c
* t
*
* Created by iAnywhere Solutions, demo user on Fri Jun 11 2004.
* Copyright ©) 2004 __MyCompanyName__. All rights reserved.
*
*/
// *******************************************************************
// Copyright 1994-2002 iAnywhere Solutions, Inc. All rights reserved.
// *******************************************************************
/* ODBCEX.C General ODBC code for Odbcex example (all platforms)
*/
#include "odbcex.h"
#include <string.h>
#if defined( MACOSX )
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <ctype.h>
#include <stdlib.h>
#include "example.h"
14
Copyright © 2004 iAnywhere Solutions, Inc.
15
Copyright © 2004 iAnywhere Solutions, Inc.
16
Copyright © 2004 iAnywhere Solutions, Inc.
void free_columns()
{
int col;
17
Copyright © 2004 iAnywhere Solutions, Inc.
Statement ) ) {
close_cursor();
return( FALSE );
}
SQLSetStmtOption( Statement, SQL_TXN_ISOLATION, 0 );
if( !retcode( SQLExecute( Statement ), Statement ) ) {
close_cursor();
return( FALSE );
}
make_columns( Statement );
return( TRUE );
}
int column_count()
{
return (NumColumns);
}
return (TRUE);
}
int exec_sql( TCHAR * cmd )
{
HSTMT stmt;
TCHAR buff[500];
if( !retcode( SQLAllocStmt( Connection, &stmt ), NULL ) )
{
return( FALSE );
}
18
Copyright © 2004 iAnywhere Solutions, Inc.
if( !test_cursor_open() ) {
return( FALSE );
}
if( relpos == 0 ) {
return( TRUE );
}
okay = retcode( SQLExtendedFetch( Statement,
SQL_FETCH_RELATIVE,
relpos,
&numfetch,
NULL ),
Statement );
if( okay ) {
if( numfetch == 0 ) {
return(FALSE);
}
}
return( okay );
}
int top()
{
SQLULEN numfetch;
int okay;
RETCODE rcode;
if( !test_cursor_open() ) {
return( FALSE );
}
okay = retcode( SQLExtendedFetch( Statement, SQL_FETCH_FIRST, 0,
&numfetch, NULL ), Statement );
if( okay ) {
rcode = SQLExtendedFetch( Statement,
SQL_FETCH_RELATIVE,
-1,
&numfetch,
NULL );
if( rcode != SQL_NO_DATA_FOUND ) {
okay = retcode( rcode, Statement );
}
}
return( okay );
}
int bottom()
{
SQLULEN numfetch;
int okay;
if( !test_cursor_open() ) {
return( FALSE );
}
okay = retcode( SQLExtendedFetch( Statement, SQL_FETCH_LAST, 0,
&numfetch, NULL ), Statement );
return( okay );
}
19
Copyright © 2004 iAnywhere Solutions, Inc.
_tcscpy(TableName, TEXT("contact"));
open_cursor();
return( TRUE );
}
int WSQLEX_Finish()
{
close_cursor();
SQLTransact( Environment, Connection, SQL_ROLLBACK );
SQLDisconnect( Connection );
SQLFreeConnect( Connection );
SQLFreeEnv( Environment );
return( TRUE );
}
20
Copyright © 2004 iAnywhere Solutions, Inc.
Appendix B: odbcex.h
/*
* odbcex.h
* t
*
* Created by iAnywhere Solutions, demo user on Fri Jun 11 2004.
* Copyright (c) 2004 __MyCompanyName__. All rights reserved.
*
*/
// *******************************************************************
// Copyright 1994-2002 iAnywhere Solutions, Inc. All rights reserved.
// *******************************************************************
/* ODBCEX.C General ODBC code for Odbcex example (all platforms)
*/
#ifndef _ODBCEXAMPLE_H_INCLUDED
#define _ODBCEXAMPLE_H_INCLUDED
#include <string.h>
#if defined( MACOSX )
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <ctype.h>
#include <stdlib.h>
#include "example.h"
//#import "OrderController.h"
#if defined( UNIX )
#include "unixodbc.h"
#elif defined( UNDER_CE )
#if !defined( UNICODE )
#define UNICODE
#endif
#include "ntodbc.h"
#if defined( SQL_NOUNICODEMAP )
// These two macros are required since Windows CE is UNICODE, but we
// define SQL_NOUNICODEMAP to get the ASCII ODBC functions
#define EX_SQL_C_TCHAR SQL_C_CHAR
#define EX_SQLTCHAR SQLCHAR
21
Copyright © 2004 iAnywhere Solutions, Inc.
22
Copyright © 2004 iAnywhere Solutions, Inc.
int top();
int bottom();
int WSQLEX_Init( void );
void WSQLEX_Process_Command( int selection );
int WSQLEX_Finish();
#endif //ODBCEXAMPLE_INCLUDED
23
Copyright © 2004 iAnywhere Solutions, Inc.
Appendix C: ContactController.m
/*
* ContactController.m
* t
*
* Created by iAnywhere Solutions, demo user on Fri Jun 11 2004.
* Copyright (c) 2004 __MyCompanyName__. All rights reserved.
*
*/
#import "ContactController.h"
@implementation ContactController
- (IBAction)connect:(id)sender
{
TCHAR * connectTxt = "Connected";
TCHAR * unconnectTxt = "Disconnected";
if ( move((long ) 1) == TRUE )
{
[self Displaytext:connectTxt];
[self Displaycolumndata];
}
else
{
isConnected = FALSE;
[self Display_systemerror:unconnectTxt];
}
}
- (IBAction)disconnect:(id)sender
{
TCHAR * unconnectTxt = "Disconnected";
TCHAR * emptytxt = " ";
int i;
if (isConnected == TRUE )
{
if ((WSQLEX_Finish()==1 ))
{
isConnected = FALSE;
[self Displaytext:unconnectTxt];
for( i = 0; i < 10; ++i )
{
[self Displaystringtext:i buff:emptytxt];
}
}
}
}
24
Copyright © 2004 iAnywhere Solutions, Inc.
- (IBAction)next:(id)sender
{
if (isConnected == TRUE )
{
if ( move(1) == TRUE )
{
[self Displaycolumndata];
}
}
}
- (IBAction)previous:(id)sender
{
if (isConnected == TRUE )
{
if ( move( -1 ) == TRUE )
{
[self Displaycolumndata];
}
}
}
- (IBAction)save:(id)sender
{
int cmdLength;
TCHAR * cmdBuffer=0;
TCHAR * updtxt = "UPDATE contact SET zip = ’";
exec_sql( cmdBuffer );
}
-(int)Displaycolumndata
{
int i;
TCHAR *data;
TCHAR * emptytxt = " ";
25
Copyright © 2004 iAnywhere Solutions, Inc.
26
Copyright © 2004 iAnywhere Solutions, Inc.
- (int)Display_systemerror:(TCHAR *)msg
{
return ([self Displaytext:msg]);
}
- (int)Displaytext:(TCHAR *)msg
{
NSString * txt = [[NSString alloc] initWithCString:msg];
@end
27
Copyright © 2004 iAnywhere Solutions, Inc.
Appendix D: ContactController.h
/*
* ContactController.h
* t
*
* Created by iAnywhere Solutions, demo user on Fri Jun 11 2004.
* Copyright (c) 2004 __MyCompanyName__. All rights reserved.
*
*/
/* ContactController */
#import <Cocoa/Cocoa.h>
#include "example.h"
#include "odbcex.h"
@interface ContactController : NSObject
{
IBOutlet id idOutlet;
IBOutlet id lastnameOutlet;
IBOutlet id firstnameOutlet;
IBOutlet id titleOutlet;
IBOutlet id streetOutlet;
IBOutlet id cityOutlet;
IBOutlet id stateOutlet;
IBOutlet id zipOutlet;
IBOutlet id phoneOutlet;
IBOutlet id faxOutlet;
IBOutlet id infoOutlet;
BOOL isConnected;
}
- (IBAction)connect:(id)sender;
- (IBAction)disconnect:(id)sender;
- (IBAction)next:(id)sender;
- (IBAction)previous:(id)sender;
- (IBAction)save:(id)sender;
- (int)Displaycolumndata;
- (void)Displaystringtext:(int)pos buff:(TCHAR *)str;
- (int)Display_systemerror:(TCHAR *)msg;
- (int)Displaytext:(TCHAR *)msg;
@end
28
Copyright © 2004 iAnywhere Solutions, Inc.
Appendix E: example.h
// *******************************************************************
// Copyright 1994-2004 iAnywhere Solutions, Inc. All rights reserved.
// *******************************************************************
/* EXAMPLE.H Common header file for all examples
*/
#ifndef _EXAMPLE_H_INCLUDED
#define _EXAMPLE_H_INCLUDED
#if defined( __WINDOWS_386__ ) \
|| defined( __NT__ ) \
|| (defined( __386__ ) && !defined( LINUX ) && !defined( SUN )) \
|| defined( _M_I386 ) \
|| defined( UNDER_CE )
// Get definition for TCHAR and related functions.
#include <windows.h>
#include <tchar.h>
#else
// Define TCHAR and related functions.
typedef char TCHAR;
#define _tcslen(s) strlen(s)
#define _tcscpy(d,s) strcpy(d,s)
#define _tcscat(d,s) strcat(d,s)
#define _tcsncpy(d,s,n) strncpy(d,s,n)
#define _stprintf sprintf
#define _vstprintf vsprintf
#define LPTSTR LPSTR
#ifndef TEXT
#define TEXT(s) s
#endif
#endif
29
Copyright © 2004 iAnywhere Solutions, Inc.
30
Copyright © 2004 iAnywhere Solutions, Inc.
ODBC.INI
[ODBC Data Sources]
ASA = ASA 9.0 Driver
[ASA]
Driver = /Applications/SQLAnywhere9/System/lib/dbodbc9_r.bundle
uid = dba
pwd = sql
start = dbeng9
dbf = /Applications/SQLAnywhere9/System/asademo.db
ThreadManager = on
[ODBC]
Trace = 0
TraceAutoStop = 0
TraceFile = /Users/demo/sql.log
TraceDLL =
31
Copyright © 2004 iAnywhere Solutions, Inc.
Appendix G: environment.plist
This file is located in $HOME/.MacOSX/environment.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ODBCINI</key>
<string>/Users/demo/Library/ODBC/odbc.ini</string>
<key>DYLD_LIBRARY_PATH</key>
<string>/Applications/SQLAnywhere9/System/lib</string>
<key>DYLD_BIND_AT_LAUNCH</key>
<string>1</string>
<key>CLASSPATH</key>
<string>/Applications/SQLAnywhere9/System/java/jodbc.jar:/Applications/SQLAnywhe
re9/shared/jConnect-5_5/classes/jconn2.jar</string>
<key>ASANY9</key>
<string>/Applications/SQLAnywhere9/System</string>
<key>ASANYSH9</key>
<string>/Users/demo/shared</string>
<key>PATH</key>
<string>/bin:/sbin:/usr/bin:/usr/sbin:/Applications/SQLAnywhere9/System/bin</str
ing>
</dict>
</plist>
32
Copyright © 2004 iAnywhere Solutions, Inc.
Legal Notice
Copyright © 2004 iAnywhere Solutions, Inc. All rights reserved. Sybase, the
Sybase logo, iAnywhere Solutions, the iAnywhere Solutions logo, Adaptive
Server, MobiLink, and SQL Anywhere are trademarks of Sybase, Inc. or its
subsidiaries. All other trademarks are property of their respective owners
The information, advice, recommendations, software, documentation, data,
services, logos, trademarks, artwork, text, pictures, and other materials
(collectively, “Materials”) contained in this document are owned by Sybase, Inc.
and/or its suppliers and are protected by copyright and trademark laws and
international treaties. Any such Materials may also be the subject of other
intellectual property rights of Sybase and/or its suppliers all of which rights are
reserved by Sybase and its suppliers.
Nothing in the Materials shall be construed as conferring any license in any
Sybase intellectual property or modifying any existing license agreement.
The Materials are provided “AS IS”, without warranties of any kind. SYBASE
EXPRESSLY DISCLAIMS ALL REPRESENTATIONS AND WARRANTIES
RELATING TO THE MATERIALS, INCLUDING WITHOUT LIMITATION, ANY
IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NON-INFRINGEMENT. Sybase makes no warranty,
representation, or guaranty as to the content, sequence, accuracy, timeliness, or
completeness of the Materials or that the Materials may be relied upon for any
reason.
Sybase makes no warranty, representation or guaranty that the Materials will be
uninterrupted or error free or that any defects can be corrected. For purposes of
this section, ‘Sybase’ shall include Sybase, Inc., and its divisions, subsidiaries,
successors, parent companies, and their employees, partners, principals, agents
and representatives, and any third-party providers or sources of Materials.
Contact Us
iAnywhere Solutions Worldwide Headquarters One Sybase Drive, Dublin,
CA, 94568 USA
Phone 1-800-801-2069 (in US and Canada)
Fax 1-519-747-4971
World Wide Web http://www.ianywhere.com
E-mail contact.us@ianywhere.com
33