SQL数据库外文翻译--数据库的工作

更新时间:2023-08-14 07:32:01 阅读量: IT计算机 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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; . . .

Note host-language variables in SET DATABASE must be preceded, as always, by a colon.

Controlling SET DATABASE scope

By default, SET DATABASE creates a handle that is global to all modules in an application.

A global handle is one that may be referenced in all host-language modules comprising the program. SET DATABASE provides two optional keywords to change the scope of a declaration:

g STATIC limits declaration scope to the module containing the SET DATABASE statement. No other program modules can see or use a database handle declared STATIC.

CHAPTER 3 WORKING WITH DATABASES

40 INTERBASE 6

EXTERN notifies gpre that a SET DATABASE statement in a module duplicates a globally-declared database in another module. If the EXTERN keyword is used, then another module must contain the actual SET DATABASE statement, or an error occurs during compilation.

The STATIC keyword is used in a multi-module program to restrict database handle access to the single module where it is declared. The following example illustrates the use of the

STATIC keyword:

EXEC SQL

SET DATABASE EMP = STATIC employee.gdb ;

The EXTERN keyword is used in a multi-module program to signal that SET DATABASE in one module is not an actual declaration, but refers to a declaration made in a different module. Gpre uses this information during preprocessing. The following example illustrates the use of the EXTERN keyword:

EXEC SQL

SET DATABASE EMP = EXTERN employee.gdb ;

If an application contains an EXTERN reference, then when it is used at run time, the actual SET DATABASE declaration must be processed first, and the database connected before other modules can access it.

A single SET DATABASE statement can contain either the STATIC or EXTERN keyword, but not both. A scope declaration in SET DATABASE applies to both COMPILETIME and RUNTIME databases.

Specifying a connection character set

When a client application connects to a database, it may have its own character set requirements. The server providing database access to the client does not know about these requirements unless the client specifies them. The client application specifies its character set requirement using the SET NAMES statement before it connects to the database.

SET NAMES specifies the character set the server should use when translating data from the database to the client application. Similarly, when the client sends data to the database, the server translates the data from the client s character set to the database s default character set (or the character set for an individual column if it differs from the database s default character set). For example, the following

statements specify that the client is using the DOS437 character set, then connect to the database:

EXEC SQL

OPENING A DATABASE

EMBEDDED SQL GUIDE 41

SET NAMES DOS437;

EXEC SQL

CONNECT europe.gdb USER JAMES PASSWORD U4EEAH ;

For more information about character sets, see the Data Definition Guide. For the complete syntax of SET NAMES and CONNECT, see the Language Reference. Opening a database

After a database is declared, it must be attached with a CONNECT statement before it can be used. CONNECT:

1. Allocates system resources for the database.

2. Determines if the database file is local, residing on the same host where the application itself is running, or remote, residing on a different host.

3. Opens the database and examines it to make sure it is valid.

InterBase provides transparent access to all databases, whether local or remote. If the database structure is invalid, the on-disk structure (ODS) number does not

correspond to the one required by InterBase, or if the database is corrupt, InterBase reports an error, and permits no further access. Optionally, CONNECT can be used to specify:

4. A user name and password combination that is checked against the server s security database before allowing the connect to succeed. User names can be up to 31 characters.

Passwords are restricted to 8 characters.

5. An SQL role name that the user adopts on connection to the database, provided that the user has previously been granted membership in the role. Regardless of role memberships granted, the user belongs to no role unless specified with this ROLE clause.

The client can specify at most one role per connection, and cannot switch roles except by reconnecting.

6. The size of the database buffer cache to allocate to the application when the default cache size is inappropriate.

Using simple CONNECT statements

In its simplest form, CONNECT requires one or more database parameters, each specifying the name of a database to open. The name of the database can be a: Database handle declared in a previous SET DATABASE statement.

CHAPTER 3 WORKING WITH DATABASES

42 INTERBASE 6

1. Host-language variable.

2. Hard-coded file name.

4Using a database handle

If a program uses SET DATABASE to provide database handles, those handles should be used in subsequent CONNECT statements instead of hard-coded names. For example,

. . .

EXEC SQL

SET DATABASE DB1 = employee.gdb ;

EXEC SQL

SET DATABASE DB2 = employee2.gdb ;

EXEC SQL

CONNECT DB1;

EXEC SQL

CONNECT DB2;

. . .

There are several advantages to using a database handle with CONNECT:

1. Long file specifications can be replaced by shorter, mnemonic handles.

2. Handles can be used to qualify table names in multi-database transactions. DSQL applications do not support multi-database transactions.

3. Handles can be reassigned to other databases as needed.

4. The number of database cache buffers can be specified as an additional CONNECT parameter.

For more information about setting the number of database cache buffers, see “Setting database cache buffers” on page 47. 4Using strings or host-language variables Instead of using a database handle, CONNECT can use a database name supplied at run time. The database name can be supplied as either a host-language variable or a hard-coded, quoted string.

The following C code demonstrates how a program accessing only a single database might implement CONNECT using a file name solicited from a user at run time: . . .

char fname[125];

. . .

printf( Enter the desired database name, including node

and path):\n );

OPENING A DATABASE

EMBEDDED SQL GUIDE 43

gets(fname);

. . .

EXEC SQL

CONNECT :fname;

. . .

Tip

This technique is especially useful for programs that are designed to work with many identically structured databases, one at a time, such as CAD/CAM or architectural databases.

MULTIPLE DATABASE IMPLEMENTATION

To use a database specified by the user as a host-language variable in a CONNECT statement in multi-database programs, follow these steps:

1. Declare a database handle using the following SET DATABASE syntax: EXEC SQL

SET DATABASE handle = COMPILETIME dbname ;

Here, handle is a hard-coded database handle supplied by the programmer, dbname is a quoted, hard-coded database name used by gpre during preprocessing.

2. Prompt the user for a database to open.

3. Store the database name entered by the user in a host-language variable.

4. Use the handle to open the database, associating the host-language variable with the handle using the following CONNECT syntax:

EXEC SQL

CONNECT : variable AS handle;

The following C code illustrates these steps:

. . .

char fname[125];

. . .

EXEC SQL

SET DATABASE DB1 = employee.gdb ;

printf("Enter the desired database name, including node

and path):\n");

gets(fname);

EXEC SQL

CONNECT :fname AS DB1;

. . .

CHAPTER 3 WORKING WITH DATABASES

44 INTERBASE 6

In this example, SET DATABASE provides a hard-coded database file name for preprocessing with gpre. When a user runs the program, the database specified in the variable, fname, is used instead. 4Using a hard-coded database names

IN SINGE-DATABASE PROGRAMS

In a single-database program that omits SET DATABASE, CONNECT must contain a hard-coded, quoted file name in the following format:

EXEC SQL

CONNECT [ host[ path]] filename ; host is required only if a program and a database file it uses reside on different nodes.

Similarly, path is required only if the database file does not reside in the current working directory. For example, the following CONNECT statement contains a hard-coded file name that includes both a Unix host name and a path name: EXEC SQL

CONNECT valdez:usr/interbase/examples/employee.gdb ;

Note Host syntax is specific to each server platform.

IMPORTANT A program that accesses multiple databases cannot use this form of CONNECT.

IN MULTI-DATABASE PROGRAMS

A program that accesses multiple databases must declare handles for each of them in separate SET DATABASE statements. These handles must be used in subsequent CONNECT statements to identify specific databases to open:

. . .

EXEC SQL

SET DATABASE DB1 = employee.gdb ;

EXEC SQL

SET DATABASE DB2 = employee2.gdb ;

EXEC SQL

CONNECT DB1;

EXEC SQL

CONNECT DB2;

. . .

Later, when the program closes these databases, the database handles are no longer in use. These handles can be reassigned to other databases by hard-coding a file name in a subsequent CONNECT statement. For example,

OPENING A DATABASE

EMBEDDED SQL GUIDE 45

. . .

EXEC SQL

DISCONNECT DB1, DB2;

EXEC SQL

CONNECT project.gdb AS DB1;

. . .

Additional CONNECT syntax

CONNECT supports several formats for opening databases to provide programming flexibility. The following table outlines each possible syntax, provides descriptions and examples, and indicates whether CONNECT can be used in programs that access single or multiple databases:

For a complete discussion of CONNECT syntax and its uses, see the Language Reference.

Syntax Description Example

Single access

Multiple access

CONNECT dbfile ; Opens a single, hard-coded database file, dbfile.

EXEC SQL

CONNECT employee.gdb ;

Yes No

CONNECT handle; Opens the database file associated with a previously declared database handle. This is the preferred CONNECT syntax.

EXEC SQL

CONNECT EMP;

Yes Yes

CONNECT dbfile AS handle;

Opens a hard-coded database file, dbfile, and assigns a previously declared database handle to it.

EXEC SQL

CONNECT employee.gdb

AS EMP;

Yes Yes CONNECT :varname AS handle;

Opens the database file stored in the host-language variable, varname, and assigns a previously declared database handle to it.

EXEC SQL CONNECT :fname AS EMP;

Yes Yes

TABLE 3.1 CONNECT syntax summary

CHAPTER 3 WORKING WITH DATABASES

46 INTERBASE 6

Attaching to multiple databases

CONNECT can attach to multiple databases. To open all databases specified in previous SET

DATABASE statements, use either of the following CONNECT syntax options: EXEC SQL

CONNECT ALL;

EXEC SQL

CONNECT DEFAULT;

CONNECT can also attach to a specified list of databases. Separate each database request from others with commas. For example, the following statement opens two databases specified by their handles:

EXEC SQL

CONNECT DB1, DB2;

The next statement opens two hard-coded database files and also assigns them to previously declared handles:

EXEC SQL

CONNECT employee.gdb AS DB1, employee2.gdb AS DB2;

Tip Opening multiple databases with a single CONNECT is most effective when a program s database access is simple and clear. In complex programs that open and close several databases, that substitute database names with host-language variables, or that assign multiple handles to the same database, use separate CONNECT statements to make program code easier to read, debug, and modify.

Handling CONNECT errors. The WHENEVER statement should be used to trap and handle runtime errors that occur during database declaration. The following C code fragment illustrates an error-handling routine that displays error messages and ends the program in an orderly fashion:

. . .

EXEC SQL

WHENEVER SQLERROR

GOTO error_exit;

. . .

OPENING A DATABASE

EMBEDDED SQL GUIDE 47

:error_exit

isc_print_sqlerr(sqlcode, status_vector);

EXEC SQL

DISCONNECT ALL;

exit(1);

. . .

For a complete discussion of SQL error handling, see Chapter 12, “Error Handling and Recovery.”

数据库的工作

这章描述怎样使用在嵌入式应用过程中的SQL语句控制数据库。有3 个数据库陈述建立并且打开进入的数据库: 确定数据库宣布一数据库经营,把这个柄与一真实数据库文件联系起来,并且选择分配给数据库的操作的参数。

确定名字选择指定客户应用为CHAR,VARCHAR 和正文一些数据使用的字符集。 服务器使用这信息从形成客户性质从而选择经营的一数据库反映性质直译, 从应用性质和更新操作的数据库的一客户那里直译。连结打开数据库,分配去它的系统资源,并且选择因那些数据库而分配操作参数。在一个程序结束之前,全部数据库必须被关闭。 一数据库可能通过使用不连接或者在附加选择对最后做的释放或者在一个程序内的返回而被关闭。

宣布数据库

在数据库之前可能被打开并且被在计划内使用,它必须首先确定数据库被宣布:

第三章数据库的工作确定数据库操作。 联系数据库与文件关于一地方和遥远节点储存的一数据库一起经营。一数据库处理一独特,缩写的别名适合一真实数据库名字。 数据库处理被使用在过程中随后连结,做释放,和随后释放陈述指定他们影响哪数据库。 除了在动态的SQL(DSQL) 应用里, 数据库处理也能在相互联系里面使用使有合格的块,或者使有差异,表格是当打开两个数据库或更多包含同等命名的表格什么时候的名字。

每个数据库处理一定在一个计划内使用的全部变量中是独特的。 数据库经营不能复制主语言保留字,并且不能是InterBase 保留字。 以下的陈述说明一个简单的数据库宣告:

EXEC SQL

SET DATABASE DB1 = ’employee.gdb’;

宣告数据库这鉴定那些文件数据库,employee. gdb,作为那些计划使用,并且分配那些数据库一个处理或者别名,DB1的数据库。

如果一个程序在一份不同于包含数据库文件的目录里运转, 然后那些说明文件名在数据库也必须包括全部路径名。例如,确定宣告指定全部的数据库的那些如下内容通向的路径employee.gdb:

EXEC SQL

SET DATABASE DB1 = ’/interbase/examples/employee.gdb’;

如果它使用的一个程序和一个数据库文件保存在不同的主人, 然后文件名说明也必须包括一个主机名。以下的宣告说明一个Unix主机名怎样被作为关于一个传输控制协议/网际协议网络的数据库文件说明表的部分包括:

EXEC SQL

SET DATABASE DB1 = ’jupiter:/usr/interbase/examples/employee.gdb’; 在使用Netbeui 协议的一个Windows 网络上,指定道路如下:

EXEC SQL

SET DATABASE DB1 = ’//venus/C:/Interbase/examples/employee.gdb’; 宣布数据库嵌入SQL指南

宣布多数据库

一个SQL程序,但不是一个DSQL程序,能同时访问多数据库。 在多数据库的计划内,数据库处理被要求。 习惯于:多数据库交易参考个别数据库。

1.使表格有合理的名字。

2.指定数据库为打开并且连结状态。

3.表明,要接受的数据库没有连接,做释放,并且随后释放。 DSQL 计划能访问单一数据库只一次,数据库处理使用连接并且从数据库那里拆开限制。

在多数据库计划内,不是每数据库都一定被宣布用一单独陈述数据库。例如,以下的代码包含两个固定的数据库陈述:

. . .

EXEC SQL

SET DATABASE DB2 = ’employee2.gdb’;

EXEC SQL

SET DATABASE DB1 = ’employee.gdb’;

. . .

当相同的表格名字在不止一次同时访问的数据库发生时,使用为表格名字处理,数据库处理必须用来把一个表格名字和另一个区别开。 数据库经营被用作给表格名字的一前缀,并且处理形式handle.table。例如,在以下代码内,数据库办理,测试和EMP,用来分清二张表格,每一个命名雇员:

. . .

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;

. . .

这数据库的使用经营只申请嵌入SQL 应用。 DSQL 应用同时不能访问多数据库。

使用在多数据库的程序里用操作处理,数据库经营一定在里指定连结陈述鉴定哪几个中的数据库打开并且准备供随后交易使用。 数据库处理被用于可能不连接,做释放,并且随后释放指定子集合的关闭的正在打开的数据库。

数据库有连结,看在第41 页上"数据库,打开"。 数据库有拆开,做释放,或者随后释放,看在第49 页上" 数据库,关闭"。为了了解更多的数据库在连接过程中处理的使用,看在第48 页"访问开的数据库"。

预处理和运行时间数据库

通常,每个数据库陈述指定单一数据库文件同一个文件交往。 当一个计划被预处理时,gpre使用被指定的文件批准计划的表格和专栏参考。 过后,当一个用户运行程序时,相同的数据库文件被访问。 不同的数据库必要时可能被指定预处理和运行时间。

使用COMPILETIME 条款,一个计划可能被用于撞上几同等组织的数据库中的任何人。 在其他情况里,当一个计划被预处理并且编辑时,一个程序将在运行时间使用的实际数据库没有出现。在这样的情况内,确定,gpre测试,数据库包括条款COMPILETIME 指定数据库能在上在期间预处理。例如,规定数据库陈述的如下内容宣布employee.gdb将在预处理期间被gpre使用:

EXEC SQL

SET DATABASE EMP = COMPILETIME ’employee.gdb’;

遵循那些关键字COMPILETIME的那些文件说明表必须总是一个坚固编码,引用线。

什么时候被确定数据库使用COMPILETIME 条款, 但是没有运行时间条款, 并且没指定,不同数据库陆续编入说明一随后陈述,相同文件数据库被使用两个适合预处理和运行时间。预处理和数据库运行时间与一起确定数据库,使用那些COMPILETIME和条款运行时间。

使用运行时间条款, 当一个数据库文件被供使用指定时, 在预处理期间,确定数据库能指定要通过包括运行时间关键字和一张运行时间文件说明表在运行时间使用的不同的数据库:

EXEC SQL

SET DATABASE EMP = COMPILETIME ’employee.gdb’

RUNTIME ’employee2.gdb’;

遵循的那些文件说明表关键字可能的那些运行时间或者一坚固编码,引用线或者易变的主语言。例如, 以下C 代码碎片促使给一数据库名字的用户,和储存名字在使用过后确定数据库的一变量内:

. . .

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; . . .

注意到主语言变量在确定数据库一定被在前,象往常一样,以冒号。

数据库的控制装置因错误,数据库创造对在应用过程中的全部模件全球的一个文件。全球文件可能在全部主语言内包括计划的模件确定数据库提供两个可选择的关键字改变一个宣告的范围:

静止限制宣告机会在控制包含定型的数据库状态。 没有其他程序模块能看见或者使用一数据库处理宣布静止。

EXTERN 通知gpre一确定的数据库陈述在一模件内复制全球宣布的在另一模内的数据库。如果EXTERN关键字被使用, 然后另一个模件必须包含实际确定数据库陈述,否则一个错误在编辑期间出现。 静止关键字在限制数据库的一多模件计划内使用办理随着宣布在哪里的单个模件的进入。以下的例子说明使用静止的关键字:

EXEC SQL

SET DATABASE EMP = STATIC ’employee.gdb’;

EXTERN关键字在用信号通知确定在一模件内的数据库的一多模件计划内使用不是一个真实宣告,但是指在一个不同的模件里做的一个宣告。 gpre在预处理期间使用这信息。 以下的例子说明使用EXTERN关键字:

EXEC SQL

SET DATABASE EMP = EXTERN ’employee.gdb’;

如果应用包含EXTERN 参考,那么被在运行时间使用, 实际确定数据库宣告必须被首先处理,并且在其他模件能访问它之前,数据库连结。 一单独的数据库确定陈述能包含的数据库或者静止或者EXTERN关键字,但不是两个都是。 宣告机会在确定数据库适用于COMPILETIME和数据库运行时间。

指定连接字符集

当客户应用连接数据库时,指定连接字符集,它可能有它自己的字符集要求。 提供数据库进入在客户的服务器不了解这些要求除非客户指定他们。在它连接数据库之前,客户应用指定使用被确定的名字陈述的它的字符集要求。

当把数据从数据库翻译到客户应用时,确定名字指定服务器应该使用的字符集。与此类似,客户把数据送到数据库, 开始数据库的默认字符集,服务器从客户的性格中翻译数据 ( 或者一个个别的专栏的字符集,如果它不同于数据库的默认字符集)。 例如,以下的陈述确切说明客户正使用DOS437字符集,然后连接数据库:

EXEC SQL

OPENING A DATABASE

EMBEDDED SQL GUIDE 41

SET NAMES DOS437;

EXEC SQL

CONNECT ’europe.gdb’ USER ’JAMES’ PASSWORD ’U4EEAH’;

对更多的关于字符集的信息来说,看数据定义指南。 完整句法确定名字并且连结,看那些参考语言。

打开一个数据库

在数据库被宣布之后,它一定附有的打开数据库一连结陈述在它可能被使用之前。

1. 连结: 为数据库分配系统资源。

2. 确定, 数据库文件如果为当地的,保存在应用它那里的相同主人,如果是遥远的,保存在不同主人。

3.打开数据库并且检查它保证它有效。 无论当地还是遥远,InterBase提供明晰的全部数据库的入口。 如果数据库结构是无效的, 在磁盘上的结构(ODS)数目不相当于InterBase需要的那个, 或者如果数据库是错误的,InterBase 报告一个错误,并且允许没有更进一步的进入。选择,连结能用来指定:

4.在允许之前的数据库服务器那加以核对的名字用户和结合密码连接成功。 用户名字可能能达到31个字符。 密码限制到8个字符。

5. SQL 角色名字用户在连接给数据库上采用,用户以前假如在角色内的会籍假若。不管准许的角色会员,用户没有属于角色,除非与这项角色条款指定。 除了通过再接通,客户能每连接在大多数那个角色确切说明,并且不能改变角色。

6. 给拖欠高速缓存尺寸是不适当的的应用分配的数据库缓冲器高速缓存的大小。

使用简单连结

在它简单形式内的陈述,连结要求一数据库参数,每一个指定打开的数据库的名字或更多。数据库的名字可能是A:

1.数据库操作在一个以前的固定的数据库陈述里声明。

2.重力加速度主语言易变。

3.严重编码的文件名。

使用一数据库处理

如果一个程序使用确定数据库提供数据库处理,处理那些应该被使用在内随后代替坚固代号陈述。 例如,

. . .

EXEC SQL

SET DATABASE DB1 = ’employee.gdb’;

EXEC SQL

SET DATABASE DB2 = ’employee2.gdb’;

EXEC SQL

CONNECT DB1;

EXEC SQL

CONNECT DB2;

本文来源:https://www.bwwdw.com/article/i8dj.html

Top