An Adaptive Server Anywhere ODBC driver is provided with our software. The ASA ODBC driver only looks enough like a driver manager to let most apps get away without a real driver manager (driver managers on UNIX are hard to come by and/or expensive). Although the ASA ODBC driver handles SQLDriverConnect and SQLConnect, it will NOT load another driver.
There are two possible ODBC driver scenarios that you may use.
- A third party ODBC Driver Manager (for example unixODBC) with the SQL Anywhere ODBC Driver
- The SQL Anywhere ODBC Driver only (If you want to use the Adaptive Server Anywhere ODBC driver without an ODBC driver manager, you can only access Adaptive Server Anywhere data sources)
Client Application
ODBC Driver Manager ASA
ODBC Driver
All Other ODBC Driver
Linking the ODBC driver manager to the ODBC driver
The libraries with the _r are for use with multi-threaded applications. If you are developing single-threaded applications, use the library without the _r.
Note: The library, executable and directory names are from verion 8. For other versions just replace the 8 with the version number.
If you are using an ODBC application that uses libodbc.so (libodbc.so.1) simply create a symlink for both libodbc.so and libodbcinst.so that they point to /opt/sybase/SYBSasa8/lib/dbodbc8_r.so.1
Example:
cd /opt/sybase/SYBSasa8/lib
ln ?s /opt/sybase/SYBSasa8/lib/dbodbc8_r.so.1 ./libodbc.so
ln ?s /opt/sybase/SYBSasa8/lib/dbodbc8_r.so.1 ./libodbcinst.so
If you are creating a custom ODBC application, you can link your application directly to dbodbc8.so.
If Adaptive Server Anywhere does not detect the presence of an ODBC driver manager, it will use .odbc.ini for data source information. It will use the first of the following files that it finds:
$ODBCINI
$ODBC_INI
$ODBCHOME/.odbc.ini
$HOME/.odbc.ini
~/.odbc.ini [according to pwuid info]
*** Note: SQLDrivers is not implemented.
A Sample .odbcinst.ini file
.odbcinst.ini example
---------------------
[ODBC Drivers]
Sybase SQL Anywhere=Installed
[Sybase SQL Anywhere]
Driver=/opt/sybase/SYBSsa8/lib/dbodbc8_r.so
A Sample .odbc.ini file
.odbc.ini example
-----------------
[ODBC Data Sources]
ASAny 8.0 Sample=asademo.db
[ASAny 8.0 Sample]
Driver=/opt/sybase/SYBSasa8/lib/dbodbc8_r.so
UID=dba
PWD=sql
DatabaseName=asademo
***Note: Please note that both ini files begin with a period. This purpose of this is to make them hidden and therefore less likely to be accidentally deleted by an end-user.
Sample .odbc.ini file format for a remote connection
For a remote connection where the client is on Linux and the server
is on some other machine, the .odbc.ini file must include at least the
"EngineName" and "CommLinks" connection parameters.
An example entry would read:
[SQLAnywhere]
Driver=/opt/sybase/SYBSasa8/lib/dbdbc8_r.so
EngineName=rickb
DatabaseName=sample
CommLinks=tcpip
Compiling and Running the odbc.c Sample
cd into the /opt/sybase/SYBSasa8/lib directory
Create a symlink for both libodbc.so and libodbcinst.so that they point
to /opt/sybase/SYBSasa8/lib/dbodbc8.so.1
Example:
ln -s /opt/sybase/SYBSasa8/lib/dbodbc8.so.1 libodbc.so
ln -s /opt/sybase/SYBSasa8/lib/dbodbc8.so.1 libodbcinst.so
cd into the /opt/sybase/SYBSasa8/cxmp directory:
/opt/sybase/SYBSasa8/cxmp
Set some environment variables as per the makeall script:
COMPILER compiler to be used
(default: gnu)
ASANY location of the ASA8 installation
(default: /opt/sybase/SYBSasa8)
ODBC location of the ODBC driver manager installation
(default: /opt/sybase/odbc)
PLATFORM the name of the target platform
(if running on Linux define as: LINUX)
Type:
make -f makeall
To run type:
./odbc
query_db example
Here is another example that basically tests an ODBC connection and outputs a verification message:
#include <stdio.h>
#include <stdlib.h>
#include "/opt/sybase/SYBSasa8/include/odbc.h"
HENV
Environment;
HDBC
Connection;
HSTMT
Statement;
static unsigned char SelectStmt[] = {"SELECT
'Connected to: ENG='||property('name')||', DBN='||db_property('name')"};
static void CheckReturn()
{
SQLSMALLINT
handletype;
SQLHANDLE
handle;
unsigned char
sqlstate[ 6 ];
unsigned char
msg[ 256 ];
if( Statement != NULL
) {
handletype = SQL_HANDLE_STMT;
handle = Statement;
} else if( Connection
!= NULL ) {
handletype = SQL_HANDLE_DBC;
handle = Connection;
} else {
handletype = SQL_HANDLE_ENV;
handle = Environment;
}
SQLGetDiagRec( handletype,
handle, 1, sqlstate, NULL, msg, sizeof( msg ), NULL );
printf( "SQL error %s
-- %s\n", sqlstate, msg );
exit( 1 );
}
int main( int argc, char * argv[] )
/*********************************/
{
int
res;
unsigned char * connect_name
= (unsigned char *)"test";
unsigned char * connect_userid
= (unsigned char *)"DBA";
unsigned char * connect_password
= (unsigned char *)"SQL";
SQLCHAR cname[256];
SQLINTEGER cnamesize;
if( SQLAllocEnv( &Environment ) != SQL_SUCCESS
) {
printf( "Unable to allocate
env\n" );
exit( 0 );
}
if( SQLAllocConnect( Environment, &Connection
) != SQL_SUCCESS ) {
printf( "Unable to allocate
connection\n" );
SQLFreeEnv( Environment
);
exit( 0 );
}
if( argc > 1 ) {
connect_name = (unsigned
char *)argv[1];
if( argc > 2 ) {
connect_userid = (unsigned char *)argv[2];
if( argc > 3 ) {
connect_password = (unsigned char *)argv[3];
}
}
}
printf( "connection parameters: DSN='%s', UID='%s', PWD='%s'\n", connect_name, connect_userid, connect_password);
res = SQLConnect( Connection,
connect_name, SQL_NTS,
connect_userid, SQL_NTS,
connect_password, SQL_NTS );
if( res != SQL_SUCCESS ) {
printf( "Unable to open
data source (ret=%d)\n", res );
CheckReturn();
exit( 0 );
}
if( SQLAllocStmt( Connection, &Statement
) != SQL_SUCCESS ) {
printf( "Unable to allocate
statement\n" );
CheckReturn();
exit( 0 );
}
if( SQLExecDirect( Statement, SelectStmt, SQL_NTS
)
!= SQL_SUCCESS ) {
printf( "Unable to execute
statement\n" );
CheckReturn();
exit( 0 );
}
res = SQLFetch(Statement);
if( res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO
) {
printf( "Unable to fetch
row\n" );
CheckReturn();
exit( 0 );
}
if( SQLGetData(Statement, 1, SQL_C_CHAR, cname,
255, &cnamesize)
!= SQL_SUCCESS) {
printf( "Unable to get
data\n" );
CheckReturn();
exit( 0 );
}
printf( "%s\n", cname );
SQLDisconnect( Connection );
SQLFreeConnect( Connection );
SQLFreeEnv( Environment );
printf( "Done.\n" );
exit( 0 );
}
To compile this sample we execute the following:
g++ -Wall -I/opt/sybase/SYBSasa8/include/ -L/opt/sybase/SYBSasa8/lib/ -DUNIX -DODBC_UNIX -lodbc -o query_db query_db.c
And then run it by entering:
./query_db
The output should look similar to this:
connection parameters: DSN='test', UID='DBA', PWD='SQL'
Connected to: ENG=rickb, DBN=asademo
Done.