没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
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:
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
SELECT EMPNO INTO :empid FROM EMP.EMPLOYEE
WHERE EMPNO = :matchid;
. . .
CHAPTER 3 WORKING WITH DATABASES
38 INTERBASE 6
IMPORTANT
This use of database handles applies only to embedded SQL applications. DSQL
applications cannot access multiple databases simultaneously.
4Using handles with operations
In multi-database programs, database handles must be specified in CONNECT
statements to identify which databases among several to open and prepare for use in
subsequent transactions.
Database handles can also be used with DISCONNECT, COMMIT RELEASE, and
ROLLBACK
RELEASE to specify a subset of open databases to close.To open and prepare a
database with CONNECT, see “Opening a database” on page 41.To close a database
with DISCONNECT, COMMIT RELEASE, or ROLLBACK RELEASE, see“Closing
a database” on page 49. To learn more about using database handles in transactions,
see “Accessing an open database” on page 48.
Preprocessing and run time databases
Normally, each SET DATABASE statement specifies a single database file to
associate with a handle. When a program is preprocessed, gpre uses the specified file
to validate the program’s table and column references. Later, when a user runs the
program, the same database file is accessed. Different databases can be specified for
preprocessing and run time when necessary.4Using the COMPILETIME clause A
program can be designed to run against any one of several identically structured
databases. In other cases, the actual database that a program will use at runtime is not
available when a program is preprocessed and compiled. In such cases, SET
DATABASE can include a COMPILETIME clause to specify a database for gpre to
test against during preprocessing. For example, the following SET DATABASE
statement declares that employee.gdb is to be used by gpre during preprocessing:
EXEC SQL
SET DATABASE EMP = COMPILETIME ’employee.gdb’;
IMPORTANT
The file specification that follows the COMPILETIME keyword must always be a
hard-coded, quoted string.
DECLARING A DATABASE
EMBEDDED SQL GUIDE 39
When SET DATABASE uses the COMPILETIME clause, but no RUNTIME
clause, and does not specify a different database file specification in a subsequent
CONNECT statement, the same database file is used both for preprocessing and run
time. To specify different preprocessing and runtime databases with SET
DATABASE, use both the COMPILETIME and
RUNTIME clauses.
4Using the RUNTIME clause
When a database file is specified for use during preprocessing, SET
DATABASE can specify a different database to use at run time by including the
RUNTIME keyword and a runtime file specification:
EXEC SQL
SET DATABASE EMP = COMPILETIME ’employee.gdb’
RUNTIME ’employee2.gdb’;
The file specification that follows the RUNTIME keyword can be either a hard-
coded, quoted string, or a host-language variable. For example, the following C code
fragment prompts the user for a database name, and stores the name in a variable that
is used later in SET DATABASE:
. . .
char db_name[125];
. . .
printf("Enter the desired database name, including node and path):\n");
gets(db_name);
EXEC SQL
SET DATABASE EMP = COMPILETIME ’employee.gdb’ RUNTIME : db_name;
. . .
剩余25页未读,继续阅读
资源评论
老帽爬新坡
- 粉丝: 82
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功