Working with Databases
This chapter describes how to use SQL statements in embedded applications to control databases. There are three database statements that set up and open databases for access: SET DATABASE declares a database handle, associates the handle with an actual database file, and optionally assigns operational parameters for the database.
SET NAMES optionally specifies the character set a client application uses for CHAR, VARCHAR, and text Blob data. The server uses this information to transliterate from a database’s default character set to the client’s character set on SELECT operations, and to transliterate from a client application’s character set to the database character set on INSERT and UPDATE operations.
g CONNECT opens a database, allocates system resources for it, and optionally assigns operational parameters for the database.All databases must be closed before a program ends. A database can be closed by using DISCONNECT, or by appending the RELEASE option to the final COMMIT or ROLLBACK in a program.
Declaring a database
Before a database can be opened and used in a program, it must first be declared with SET DATABASE to:
CHAPTER 3 WORKING WITH DATABASES. Establish a database handle. Associate the database handle with a database file stored on a local or remote node.A database handle is a unique, abbreviated alias for an actual database name. Database handles are used in subsequent CONNECT, COMMIT RELEASE, and ROLLBACK RELEASE statements to specify which databases they should affect. Except in dynamic SQL (DSQL) applications, database handles can also be used inside transaction blocks to qualify, or differentiate, table names when two or more open databases contain identically named tables.
Each database handle must be unique among all variables used in a program. Database handles cannot duplicate host-language reserved words, and cannot be InterBase reserved words.The following statement illustrates a simple database declaration:
EXEC SQL
SET DATABASE DB1 = ’employee.gdb’;
This database declaration identifies the database file, employee.gdb, as a database the program uses, and assigns the database a handle, or alias, DB1.
If a program runs in a directory different from the directory that contains the database file, then the file name specification in SET DATABASE must include a full path name, too. For example, the following SET DATABASE declaration specifies the full path to employee.gdb:
server error翻译EXEC SQL
SET DATABASE DB1 = ’/interbase/examples/employee.gdb’;
If a program and a database file it uses reside on different hosts, then the file name specification must also include a host name. The following declaration illustrates how a Unix host name is included as part of the database file specification on a TCP/IP network:
EXEC SQL
SET DATABASE DB1 = ’jupiter:/usr/interbase/examples/employee.gdb’;
On a Windows network that uses the Netbeui protocol, specify the path as follows:
EXEC SQL
SET DATABASE DB1 = ’//venus/C:/Interbase/examples/employee.gdb’;
DECLARING A DATABASE
EMBEDDED SQL GUIDE 37
Declaring multiple databases
An SQL program, but not a DSQL program, can access multiple databases at the same time. In multi-database programs, database handles are required. A handle is used to:
1. Reference individual databases in a multi-database transaction.
2. Qualify table names.
3. Specify databases to open in CONNECT statements.
Indicate databases to close with DISCONNECT, COMMIT RELEASE, and ROLLBACK RELEASE.
DSQL programs can access only a single database at a time, so database handle use is restricted to connecting to and disconnecting from a database.
In multi-database programs, each database must be declared in a separate SET DATABASE statement. For example, the following code contains two SET DATABASE statements:
. . .
EXEC SQL
SET DATABASE DB2 = ’employee2.gdb’;
EXEC SQL
SET DATABASE DB1 = ’employee.gdb’;
. . .
4Using handles for table names
When the same table name occurs in more than one simultaneously accessed database, a database handle must be used to differentiate one table name from another. The database handle is used as a prefix to table names, and takes the form handle.table.
For example, in the following code, the database handles, TEST and EMP, are used to distinguish between two tables, each named EMPLOYEE:
. . .
EXEC SQL
DECLARE IDMATCH CURSOR FOR
SELECT TESTNO INTO :matchid FROM TEST.EMPLOYEE
WHERE TESTNO > 100;
EXEC SQL
DECLARE EIDMATCH CURSOR FOR
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论