The following example steps you through the process of creating a database object, connecting to a datasource, creating a table, and moving data between the database and IDL. The example uses a server called SQLAnywhere; you will need to replace references to the SQLAnywhere server with references to your own specific database server. To work through this example, you will need to be able to connect to and log on to your database server.

Create a database object. Enter the following at the IDL command prompt:

oDB = obj_new('IDLDBDatabase')

Use the GetDatasources method to discover the names of the datasources available on your system, the print the list to your command log window:

sources = oDB->GetDatasources()
PRINT, sources.datasource, FORMAT=’(a)’

IDL will print something like the following:

SybaseDBLib
Sybase
SQLAnywhere
Oracle
Ingres
Informix
MSSQLServer

Connect to the SQLAnywhere server. (Substitute your own datasource, username, and password.)

oDB->Connect, DataSource = 'SQLAnywhere', $
  user=username, password=passwd

Get a list of the available tables:

tables = oDB->GetTables()
PRINT, tables.name, FORMAT=’(a)’

IDL will print something like the following:

sysalternates sysarticles syscolumns syspublications sysreferences systypes sysusers
mydata

Create a new table named “im_info” using SQL commands:

oDB->ExecuteSQL, $
  "create table im_info (id integer, x integer," + $
  "y integer, data image, name char(50))"

Now create a Recordset object and connect to the table you just created:

oRS = obj_new('IDLdbRecordSet', oDB, table='im_info')

Add a record to the object. This record contains four fields that describe an image: the width of the image, the height of the image, the image data itself, and the name of the image.

oRS->AddRecord, 1, 400, 400, BYTSCL(DIST(400)), 'first image'

Move the current location in the table (the cursor position) to the first row:

status = oRS->MoveCursor(/FIRST)

You can check the value of the variable status and report on whether the move was successful:

IF(status NE 1) THEN BEGIN
  PRINT, 'Error moving database cursor' RETURN
  ENDIF

Retrieve the information from this record into IDL variables:

X = oRS->GetField(1)X size of image
Y = oRS->GetField(2)Y size of image
image = oRS->GetField(3)Image
data name = oRS->getField(4)Image name

Create an IDL window to display the image:

WINDOW, COLORS=-5, TITLE=name, XSIZE=x, YSIZE=y

Reform the image into two dimensions (ODBC data is stored as a one-dimensional stream of bytes):

image = REFORM(image, 400, 400)

Display the image:

TVSCL, image

Delete the im_info table and destroy the database objects:

oDB->ExecuteSQL, 'drop table im_info' OBJ_DESTROY, oDB