Accessing Databases (Using Dynamic SQL)

From InterBase

Go Up to DSQL Limitations (Using Dynamic SQL)


Using standard SQL syntax, a DSQL application can only use one database handle per source file module, and can, therefore, only be connected to a single database at a time. Database handles must be declared and initialized when an application is preprocessed with gpre. For example, the following code creates a single handle, db1, and initializes it to zero:

#include "ibase.h"
isc_db_handle db1;
. . .
db1 = 0L;

After a database handle is declared and initialized, it can be assigned dynamically to a database at run time as follows:

char dbname[129];
. . .
prompt_user("Name of database to open: ");
gets(dbname);
EXEC SQL
SET DATABASE db1 = :dbname;
EXEC SQL
CONNECT db1;
. . .

The database accessed by DSQL statements is always the last database handle mentioned in a SET ­DATABASE command. A database handle can be used to connect to different databases as long as a previously connected database is first disconnected with DISCONNECT. DISCONNECT automatically sets database handles to NULL. The following statements disconnect from a database, zero the database handle, and connect to a new database:

EXEC SQL
DISCONNECT db1;
EXEC SQL
SET DATABASE db1 = 'employee.ib';
EXEC SQL
CONNECT db1;

To access more than one database using DSQL, create a separate source file module for each database, and use low-level API calls to attach to the databases and access data. For more information about accessing databases with API calls, see the API Guide. For more information about SQL database statements, see Working with Databases.

Advance To: