版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、working with databasesthis 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 option
2、ally 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 databases default character set to the clients character set on select opera
3、tions, and to transliterate from a client applications 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 progr
4、am 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 databasebefore 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
5、 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 data
6、bases 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
7、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 sqlset database db1 = employee.gdb;this database declaration identifies the database file, employee.gdb, as a datab
8、ase 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 declarati
9、on specifies the full path to employee.gdb:exec sqlset 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 inc
10、luded as part of the database file specification on a tcp/ip network:exec sqlset database db1 = jupiter:/usr/interbase/examples/employee.gdb;on a windows network that uses the netbeui protocol, specify the path as follows:exec sqlset database db1 = /venus/c:/interbase/examples/employee.gdb;declaring
11、 a databaseembedded sql guide 37declaring multiple databasesan 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. qual
12、ify 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 m
13、ulti-database programs, each database must be declared in a separate set database statement. for example, the following code contains two set database statements:. . .exec sqlset database db2 = employee2.gdb;exec sqlset database db1 = employee.gdb;. . .4using handles for table nameswhen the same tab
14、le 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 em
15、p, are used to distinguish between two tables, each named employee:. . .exec sqldeclare idmatch cursor forselect testno into :matchid from test.employeewhere testno 100;exec sqldeclare eidmatch cursor forselect empno into :empid from emp.employeewhere empno = :matchid;. . .chapter 3 working with dat
16、abases38 interbase 6important this use of database handles applies only to embedded sql applications. dsql applications cannot access multiple databases simultaneously.4using handles with operationsin multi-database programs, database handles must be specified in connect statements to identify which
17、 databases among several to open and prepare for use in subsequent transactions.database handles can also be used with disconnect, commit release, and rollbackrelease 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 cl
18、ose 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 databasesnormally, each set database statement specifies a singl
19、e database file to associate with a handle. when a program is preprocessed, gpre uses the specified file to validate the programs 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
20、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
21、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 sqlset database emp = compiletime employee.gdb;important the file specifi
22、cation that follows the compiletime keyword must always be a hard-coded, quoted string.declaring a databaseembedded sql guide 39when 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 sa
23、me database file is used both for preprocessing and run time. to specify different preprocessing and runtime databases with set database, use both the compiletime andruntime clauses.4using the runtime clausewhen a database file is specified for use during preprocessing, set database can specify a di
24、fferent database to use at run time by including the runtime keyword and a runtime file specification:exec sqlset database emp = compiletime employee.gdbruntime employee2.gdb;the file specification that follows the runtime keyword can be either a hard-coded, quoted string, or a host-language variabl
25、e. 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_name125;. . .printf(enter the desired database name, including node and path):n); gets(db_name);exec sqlset database emp = compiletime
26、 employee.gdb runtime : db_name;. . .note host-language variables in set database must be preceded, as always, by a colon.controlling set database scopeby 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-
27、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
28、 with databases40 interbase 6extern 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 k
29、eyword 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 thestatic keyword:exec sqlset database emp = static employee.gdb;the extern keyword is used in a multi-module program to signal that set
30、 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 sqlset database emp = extern employee.gdb;if an application contains an
31、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 dat
32、abase applies to both compiletime and runtime databases.specifying a connection character setwhen 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 s
33、pecifies 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 sen
34、ds data to the database, the server translates the data from the clients character set to the databases default character set (or the character set for an individual column if it differs from the databases default character set). for example, the following statements specify that the client is using
35、 the dos437 character set, then connect to the database:exec sqlopening a databaseembedded sql guide 41set names dos437;exec sqlconnect 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, se
36、e the language reference.opening a databaseafter 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 r
37、unning, 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 requ
38、ired 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 servers security database before allowing the connect to succeed. user names can
39、 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 wit
40、h 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 statementsin its simplest form, connect
41、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 databases42 interbase 61. host-language variable.2. hard-coded file name.4using a database
42、handleif 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 sqlset database db1 = employee.gdb;exec sqlset database db2 = employee2.gdb;exec sqlconnect db1;exec sqlconnect db2;. . .
43、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 r
44、eassigned 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 inste
45、ad 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 fi
46、le name solicited from a user at run time:. . .char fname125;. . .printf(enter the desired database name, including nodeand path):n);opening a databaseembedded sql guide 43gets(fname);. . .exec sqlconnect :fname;. . .tip this technique is especially useful for programs that are designed to work with
47、 many identically structured databases, one at a time, such as cad/cam or architectural databases.multiple database implementationto 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 usi
48、ng the following set database syntax:exec sqlset database handle = compiletime dbname;here, handle is a hard-coded database handle supplied by the programmer, dbnameis a quoted, hard-coded database name used by gpre during preprocessing.2. prompt the user for a database to open.3. store the database
49、 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 sqlconnect : variable as handle;the following c code illustrates these steps:. . .char fname125;. . .exec sqlse
50、t database db1 = employee.gdb;printf(enter the desired database name, including nodeand path):n);gets(fname);exec sqlconnect :fname as db1;. . .chapter 3 working with databases44 interbase 6in this example, set database provides a hard-coded database file name for preprocessing with gpre. when a use
51、r runs the program, the database specified in the variable, fname, is used instead. 4using a hard-coded database namesin singe-database programsin a single-database program that omits set database, connect must contain a hard-coded, quoted file name in the following format:exec sqlconnect host path
52、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
53、 unix host name and a path name:exec sqlconnect 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 programsa program that accesses multiple databases mu
54、st 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 sqlset database db1 = employee.gdb;exec sqlset database db2 = employee2.gdb;exec sqlconnect db1;exec sqlconnect db2;.
55、. .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 databaseembedded sql guide 45. . .exec sqldisconnect db1, db2;exec sqlc
56、onnect project.gdb as db1;. . .additional connect syntaxconnect 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
57、single or multiple databases:for a complete discussion of connect syntax and its uses, see the language reference.syntax description examplesingle accessmultiple accessconnect dbfile; opens a single, hard-coded database file, dbfile.exec sqlconnect employee.gdb;yes noconnect handle; opens the databa
58、se file associated with a previously declared database handle. this is the preferred connect syntax.exec sql connect emp;yes yesconnect dbfile as handle;opens a hard-coded database file, dbfile, and assigns a previously declared database handle to it.exec sqlconnect employee.gdbas emp;yes yes connect :varname as handle;open
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2019-2025年中國(guó)高速公路行業(yè)市場(chǎng)前景預(yù)測(cè)及投資戰(zhàn)略研究報(bào)告
- 2025年中國(guó)保險(xiǎn)從業(yè)培訓(xùn)行業(yè)市場(chǎng)深度評(píng)估及投資戰(zhàn)略規(guī)劃報(bào)告
- 2024年跨境電商物流配合契約3篇
- 二零二五年度公共機(jī)構(gòu)合同能源管理實(shí)施細(xì)則模板3篇
- 2025年中國(guó)開(kāi)源軟件行業(yè)發(fā)展監(jiān)測(cè)及發(fā)展趨勢(shì)預(yù)測(cè)報(bào)告
- 2024年職場(chǎng)競(jìng)業(yè)禁止規(guī)范合同書(shū)版B版
- 2025年度安全生產(chǎn)隱患排查治理服務(wù)合同范本3篇
- 2024版二手房買賣合同范本一
- 二零二五年廣播設(shè)備更新?lián)Q代與安裝合同2篇
- 二零二五年度健身行業(yè)投資合作承包協(xié)議書(shū)3篇
- 2023年7月黑龍江高中學(xué)業(yè)水平合格性考試歷史試卷真題(含答案詳解)
- 2024年血透管路行業(yè)技術(shù)趨勢(shì)分析
- 美術(shù)年終總結(jié)匯報(bào)
- 數(shù)字孿生技術(shù)與MES系統(tǒng)的融合
- 人才梯隊(duì)(人才庫(kù)、人才盤點(diǎn))建設(shè)方案
- 廣西柳州市2023-2024學(xué)年四年級(jí)上學(xué)期期末考試語(yǔ)文試卷
- 《芯片制造工藝》課件
- 中山大學(xué)研究生中特考試大題
- 手術(shù)室護(hù)理實(shí)踐指南術(shù)中低體溫預(yù)防
- 鋼管混凝土柱計(jì)算
- 四川省成都市2022-2023學(xué)年六年級(jí)上學(xué)期語(yǔ)文期末考試試卷(含答案)5
評(píng)論
0/150
提交評(píng)論