CIS275 - Chapter 8: Database Programming Flashcards

1
Q

____ contain control flow statements that determine the execution order of program steps.

A

Imperative languages

Control flow statements include loops for repeatedly executing code and conditionals for conditionally executing code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

_____ are composed of procedures, also called functions or subroutines.

A

Procedural languages

Most languages developed prior to 1990 are procedural. Ex: C and COBOL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

_____ organize code into classes.

A

Object-oriented languages

A class combines variables and procedures into a single construct. Most languages developed since 1990 are object-oriented. Ex: Java, Python, and C++.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

_____ do not contain control flow statements.

A

Declarative languages

Each statement declares what result is desired, using logical expressions, rather than how the result is processed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Compilers for declarative languages

A

optimizers

the compiler determines an optimal way to process each declarative statement.

SQL is the leading example of a declarative language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Building applications with both SQL and a general-purpose language is called _____.

A

database programming

Database programming presents two challenges:

Syntax gap. The syntax of SQL and most other languages are quite different. Ex: Groups of statements, called blocks, are fundamental to most procedural and object-oriented languages. SQL has no blocks.

Paradigm gap. Query processing is fundamentally different in SQL and procedural or object-oriented languages. Ex: A result set with many rows is processed in a single SQL statement, but usually requires loops and conditional statements in other languages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. A declarative language specifies what result is desired. The SQL selects all airline names and flight numbers departing from JFK airport after noon.
  2. A procedural language specifies how the result is processed. The code fragment is written in C.
  3. printf() displays column headings. The string containing % characters specifies the heading format.
A
  1. The for loop checks every row of the Flight table. Variable i is the row number.
  2. The if statement selects flights departing from JFK airport after noon.
  3. printf() displays the airline name and flight number of selected flights.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

_____ codes SQL statements directly in a program written in another language.

A

Embedded SQL

The keywords EXEC SQL precede all SQL statements so the compiler can distinguish SQL from other statements. Ex: Embedded SQL in C for Oracle Database is called Pro*C.

Embedded SQL allows the programmer to write SQL statements directly within another programming language. This approach is conceptually simple but suffers three problems:

Gaps. Most programming today uses object-oriented languages. The syntax and paradigm gap between object-oriented language and SQL is wide, so programs are difficult to write and maintain.

Compile steps. The SQL statements and host language must be compiled with different compilers, in two steps.

Network traffic. Application programs usually run on a client computer, connected to the database server via a network. With embedded SQL, each query generates a ‘round-trip’ from client to server, increasing network traffic and execution time.

For the above reasons, embedded SQL is no longer widely used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

_____ extends the SQL language with control flow statements, creating a new programming language.

A

Procedural SQL

Procedural SQL is limited compared to general-purpose languages, however, and is used primarily for database applications. Ex: In Oracle Database, procedural SQL is called PL/SQL.

Procedural SQL has several advantages over embedded SQL:

Gaps. Procedural SQL gracefully extends SQL. The syntax and paradigm gaps are minimal so programs are relatively easy to write and maintain.

Compile steps. Procedural SQL is compiled in one step rather than two.

Network traffic. Client applications typically call SQL procedures for execution on the database server. Each procedure executes multiple database operations, reducing network traffic and execution time.

Optimization level. The database optimizes entire procedures rather than individual queries, resulting in better optimization and faster query execution.

Procedural SQL languages vary significantly by database and do not offer the full capabilities of general-purpose languages. Ex: Most procedural SQL languages are not object-oriented. For these reasons, procedural SQL is commonly used for limited database tasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

a library of procedures or classes.

A

application programming interface, or API

The library links an application programming language to a computer service, such as a database, email, or web service. The procedure or class declarations are written in the application programming language. Ex: JDBC is a library of Java classes that access relational databases.

APIs address the limitations of embedded SQL and procedural SQL:

Gaps. Since API procedures and class declarations are written in the application language, applications are compiled in one step, the syntax gap disappears, and the paradigm gap is reduced.

Applications. Database APIs are available for general-purpose, object-oriented languages. Applications are not limited to database processing tasks and procedural languages.

Database independence. Unlike procedural SQL, language and API syntax is independent of data source.

For the reasons above, APIs are the most commonly used database programming technique.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. The reservationConnection object connects the program to the reservation database.
  2. The listFlights object contains the SELECT statement and connects to the Reservation database.
  3. The flightReader object points to individual rows of the result table.
A
  1. ExecuteReader() executes the SELECT statement stored in the listFlights object.
  2. Read() moves flightReader to the next result table row. The while loop repeats until all rows have been read.
  3. GetInt32(0) and GetString(1) return the first and second columns of the current FlightReader row.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Embedded SQL statements appear in programs written in another language, called the _____.

A

host language

To distinguish SQL from host language statements, embedded SQL begins with the keyword EXEC SQL, followed by an SQL statement and a semicolon.

Programs containing embedded SQL are compiled in two steps:

A precompiler translates SQL statements into host language statements and function calls.

The host language compiler translates the host language program to an executable program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

a variant of embedded SQL designed for Java.

A

SQLJ

SQLJ is similar to standard embedded SQL, but the syntax is adapted to Java. SQLJ is not widely supported, since object-oriented languages like Java commonly use an API rather than embedded SQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. The C program begins with int main() and ends with return 0.
  2. printf() is a C statement that outputs text. \n prints a new line.
A
  1. An embedded SQL statement begins with EXEC SQL and ends with a semicolon. The UPDATE statement executes within the C program.
  2. The precompiler translates embedded SQL to C statements and function calls.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
  1. The CONNECT TO statement creates a connection to the database at internet address 100.320.420.999.
  2. FlightConnection is active.
A
  1. Subsequent queries refer to the FlightConnection database.
  2. When the connection is no longer needed, release the associated resources.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

a host language variable that appears in SQL statements.

A

shared variable

The results of SELECT statements are assigned to shared variables for further processing in the host language. Shared variables must be declared between BEGIN DECLARE SECTION and END DECLARE SECTION statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. Three shared variables are declared between BEGIN DECLARE SECTION and END DECLARE SECTION.
  2. The C code allows the user to input a flight number. The shared variable flight is assigned the number.
  3. The query selects rows with column FlightNumber equal to the shared variable flight.
A
  1. The shared variable airline is assigned AirlineName. The shared variable airport is assigned AirportCode.
  2. The C code outputs the result of the SELECT statement.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

an embedded SQL variable that identifies an individual row of a result table.

A

cursor

Cursors are necessary to process queries that return multiple rows. Cursors are managed with four embedded SQL statements:

DECLARE CursorName CURSOR FOR Statement; creates a cursor named CursorName that is associated with the query Statement.

OPEN CursorName; executes the query associated with CursorName and positions the cursor before the first row of the result table.

FETCH FROM CursorName INTO :SharedVariable1, :SharedVariable2, … ; advances CursorName to the next row of the result table and copies selected values into the shared variables.

CLOSE CursorName; releases the result table associated with the cursor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
  1. DECLARE CURSOR creates a cursor named FlightCursor that is associated with the SELECT query.
  2. OPEN executes the query and positions FlightCursor before the result table’s first row.
  3. FETCH moves the cursor to first row and assigns column values to corresponding shared variables.
A
  1. INCLUDE declares SQLSTATE to a C program. The value “00000” indicates the cursor is pointing to a valid row.
  2. While the cursor points to a valid row, output the query result and fetch the next row.
  3. When the cursor moves past the last row, the while loop terminates and the cursor is released.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Embedded SQL statements that are generated at run-time are called _____.

A

dynamic SQL

dynamic SQL is compiled as the program executes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Embedded SQL statements that are fixed in program code are called _____.

A

static SQL

Static SQL statements are compiled by the precompiler, while dynamic SQL is compiled as the program executes. For this reason, static SQL executes faster than dynamic SQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q
  1. Two shared variables are declared. queryString will later contain an SQL statement.
  2. The user inputs an SQL query, which is saved in queryString.
  3. The query is compiled and saved as QueryName.
  4. The user inputs flight number 692. The query executes and deletes rows containing flight number 692.
A
  1. Two shared variables are declared. queryString will later contain an SQL statement.
  2. The user inputs an SQL query, which is saved in queryString.
  3. The query is compiled and saved as QueryName.
  4. The user inputs flight number 692. The query executes and deletes rows containing flight number 692.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
A

(A) TO
(B) RoomConnection
(C) USER
(D) SET
(E) RoomConnection

(A) TO: Specifies the server address in a CONNECT command. In this case, 127.0.0.1 is the server address.

(B) RoomConnection: Connection name in a CONNECT command that follows AS.

(C) USER: Gives the login name in a CONNECT command.

(D) SET: Chooses which connection to use (more than one may be open at a time).

(E) RoomConnection: Connection name in the DISCONNECT command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
A

Expected:
(A) BEGIN
(B) DECLARE
(C) END
(D) SECTION
(E) roomSize

(A), (B): BEGIN DECLARE SECTION: Begins the shared variable declaration section.

(C), (D): END DECLARE SECTION: Ends the shared variable declaration section.

(E): roomSize: Following INTO, names the variable into which the value of the Capacity column is saved. The colon is required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
A

Expected:
(A) CURSOR FOR
(B) OPEN
(C) FETCH
(D) FROM
(E) RoomCursor

(A) DECLARE RoomCursor CURSOR FOR: Creates a cursor named RoomCursor.

(B) OPEN RoomCursor: Prepares the cursor for use.

(C) FETCH: Gets one result from the cursor.

(D) FROM: Names the cursor to use in a FETCH statement.

(E) RoomCursor: Names the cursor to use in the CLOSE statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

an extension of the SQL language that includes procedures, functions, conditionals, and loops.

A

Procedural SQL

Procedural SQL is intended for database applications and does not offer the full capabilities of general-purpose languages such as Java, Python, and C.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Procedural SQL can, in principle, be used for complete programs. More often, however, the language is used to create procedures that interact with the database and accomplish limited tasks. These procedures are compiled by and stored in the database, and therefore are called_____.

A

stored procedures

Stored procedures can be called from a command line or a host program written in another language. Host program calls to stored procedures can be coded with embedded SQL or built into an API.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

a standard for procedural SQL that extends the core SQL standard.

A

SQL/Persistent Stored Modules (SQL/PSM

SQL/PSM is implemented in many relational databases with significant variations and different names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

A stored procedure is declared with a _____ statement, which includes the procedure name, optional parameter declarations, and the procedure body.

A

CREATE PROCEDURE

40
Q

A _____ has an optional IN, OUT, or INOUT keyword, a ParameterName, and a Type.

A

parameter declaration

41
Q

The _____ consists of either a simple statement, such as SELECT and DELETE, or a compound statement.

A

procedure body

Compound statements, described below, are commonly used in stored procedures to code complex database tasks.

42
Q

A stored procedure is executed with a _____.

A

CALL statement.

The CALL statement includes the procedure name and a parameter list. The types of the CALL parameters must be compatible with the types of the corresponding CREATE PROCEDURE parameters.

43
Q
  1. The FlightCount stored procedure has an input parameter airline and an output parameter quantity.
  2. The stored procedure body is a single SELECT statement.
  3. The INTO clause assigns the COUNT(*) value to the quantity parameter.
A
  1. The stored procedure is called from the command line. airline is assigned ‘British Airways’. @result is assigned the resulting quantity value.
  2. The value of the user-defined variable @result is displayed with a SELECT statement. The Flight table has 41 British Airways flights.
44
Q

an SQL variable that must begin with an @ character.

A

user-defined variable

When called from a different programming language, call parameter syntax varies greatly, depending on the language and API.

45
Q
A
46
Q

a series of statements between a BEGIN and an END keyword.

A

compound statement

The statements may be variable declarations, assignment statements, if statements, while loops, and other common programming constructs.

47
Q

The _____ statement creates variables for use inside stored procedures.

A

DECLARE statement

A variable has a name, data type, and an optional default value.

48
Q

The _____ assigns a variable with an expression.

A

SET statement

The variable must be declared prior to the SET statement, and the expression may be any valid SQL expression.

49
Q

The _____ is similar to statements in other programming languages.

A

IF statement

The statement includes a logical expression and a statement list. If the expression is true, the statements in the list are executed. If the expression is false, the statements are ignored.

IF statements have optional ELSEIF and ELSE clauses. ELSEIF and ELSE clauses are executed when the expression in the IF clause is false, as in other programming languages.

50
Q

The _____ also includes an expression and a statement list.

A

WHILE statement

The statement list repeatedly executes as long as the expression is true. When the expression is false, the WHILE statement terminates, and the statement following END WHILE executes.

51
Q
  1. The AddFlights() stored procedure inserts new flights into the Flight table. The body is a compound statement.
  2. The declare statements create and initialize two variables. departTime is assigned the startTime parameter. flightNum is assigned 540.
  3. The WHILE loop repeats as long as departTime is ≤ endTime.
A
  1. Each pass through the loop inserts a new flight.
  2. flightNum is incremented by 100. If departTime is before noon, add 30 minutes. If departTime is noon or later, add an hour.
52
Q

A _____ is like a stored procedure that returns a single value.

A

stored function

53
Q
  1. The cursor named flightCursor retrieves data for all flights.
  2. The finishedReading variable is initially FALSE. The handler sets finishedReading to TRUE when the cursor moves past the last row.
  3. OPEN executes flightCursor’s associated query and sets the cursor prior to the first result table row.
  4. FETCH moves the cursor to the first row and assigns column values to procedure variables.
A
  1. The WHILE loop repeats until the cursor moves past the last row.
  2. The IF statement changes the departure time for the selected flight.
  3. FETCH moves the cursor to the next row.
  4. CLOSE releases cursor resources when no longer needed.
54
Q

A stored function is declared with a _____ statement.

A

CREATE FUNCTION

55
Q
  1. The stored function computes an employee’s tax and returns an integer. The function reads SQL data with a SELECT statement.
  2. The employee’s salary is retrieved and assigned to the income variable.
  3. Tax is computed as 10% of income exceeding $25,000 and returned.
A
  1. Stored functions are called within any SQL expression, like built-in functions.
  2. Lisa Ellison’s salary is $40,000. Tax is ($40,000 - $25,000) * 10% = $1,500, displayed as a result table.
56
Q
A
57
Q

a stored procedure or a stored function

A

trigger

Triggers have neither parameters nor a return value. Triggers read and write tables but do not communicate directly with a calling program.

Triggers are not explicitly invoked by a CALL statement or within an expression. Instead triggers are associated with a specific table and execute whenever the table is changed.

Triggers are used to enforce business rules automatically as data is updated, inserted, and deleted.

58
Q

The _____ statement specifies a TriggerName followed by four required keywords:

A

CREATE TRIGGER

ON TableName identifies the table associated with the trigger.

INSERT, UPDATE, or DELETE indicates that the trigger executes when the corresponding SQL operation is applied to the table.

BEFORE or AFTER determines whether the trigger executes before or after the insert, update, or delete operation.

FOR EACH ROW indicates the trigger executes repeatedly, once for each row affected by the insert, update, or delete operations.

59
Q
  1. The ExamGrade trigger executes before inserts to the Exam table.
  2. One insert statement can create multiple rows. The trigger executes once for each new row.
  3. The trigger automatically determines exam grade and updates the Grade column in new table rows.
A
  1. Inserting new rows causes the ExamGrade trigger to execute before the rows are inserted.
  2. The Grade column contains values set by the ExamGrade trigger.
60
Q
A
61
Q
A

Expected:
(A) CREATE
(B) IN
(C) DEFAULT
(D) SET
(E) WHILE
(F) END

(A) CREATE PROCEDURE begins the definition of a stored procedure.

(B) IN roomName VARCHAR(40) declares roomName as an input parameter with type VARCHAR(40).

(C) DECLARE n INT DEFAULT 1; assigns the initial value of n to be 1.

(D) SET n = n + 1; sets the value of n to n + 1.

(E) END WHILE ends a WHILE statement.

(F) END ends the definition of the stored procedure AddSeats.

62
Q
A

Expected:
(A) CREATE
(B) IN
(C) INT
(D) BEGIN
(E) SET
(F) WHILE

(A) CREATE PROCEDURE begins the definition of a stored procedure.

(B) IN howMany INT declares howMany as an input parameter.

(C) IN howMany INT declares the type of howMany to be INT.

(D) BEGIN begins a compound statement.

(E) SET n = n + 1; sets the value of n to n + 1.

(F) END WHILE ends a WHILE statement.

63
Q
A

Expected:
(A) CURSOR
(B) HANDLER
(C) FROM
(D) INTO
(E) IF
(F) CLOSE

(A) CURSOR follows the cursor name when declaring a cursor.

(B) HANDLER follows CONTINUE when declaring a handler.

(C) FETCH FROM roomCursor retrieves the next result from roomCursor.

(D) INTO follows the cursor name to copy selected values into nam.

(E) IF follows END to end the IF statement.

(F) CLOSE closes the cursor before the procedure exits.

64
Q
A
65
Q

An _____ specifies the interaction between an application and a computer service, such as a database, email, or web service.

A

application programming interface (API)

If the application is written in a procedural language like C, the API specifies procedure calls. If the application is written in an object-oriented language like Java, the API specifies classes.

66
Q
A
67
Q
  1. The FlightCount procedure is written in procedural SQL and stored in the Reservation database.
  2. The java.sql namespace implements the JDBC API.
  3. The reservation object opens a connection to the reservation database. close() releases the connection.
A
  1. prepareCall() compiles CALL FlightCount and saves the statement in the flightCall object. ? characters are placeholders for parameters.
  2. Java code inputs an airline name to the Java variable airlineName.
  3. The airline parameter is set to airlineName, the quantity parameter is registered as an integer, and the query is executed.
  4. The Java variable total is assigned the Quantity value and displayed.
68
Q
A
69
Q
  1. Java applications communicate via the JDBC API with the JDBC driver manager.
  2. The driver manager communicates with each data source via a different driver.
  3. If an API has no driver for a data source, the API may communicate indirectly via another API.
A
  1. The ODBC driver for JDBC communicates with Excel and CSV data sources via the ODBC driver manager.
70
Q
A
71
Q

Database API implementations typically contain two software layers:

A _____ connects directly to the database.

A

driver

Although all relational databases support standard SQL, implementations vary somewhat, and most databases offer non-standard extensions. Consequently, a different driver is necessary for each database.

72
Q

Database API implementations typically contain two software layers:

The driver manager connects the application to the drivers.

A

driver manager

The application communicates with the driver manager using API procedure calls. The driver manager forwards each call to the correct driver. When one application connects to several databases, the driver manager selects a driver based on the active connection.

73
Q

a file containing Python classes, functions, or variables.

A

module

Python programs must connect to a database prior to executing queries.

74
Q
  1. The import statements make Connector/Python code and errorcode module available to the program.
  2. The mysql.connector.connect() function creates a MySQLConnection object named reservationConnection.
  3. If the connection fails, the except block executes and prints an error message.
A
  1. If the connection succeeds, the else block executes. Subsequent queries use the reservationConnection object.
  2. When database processing is finished, close() releases the connection.
75
Q
A
76
Q
  1. The cursor() method creates a cursor called flightCursor, associated with the Reservation database.
  2. The execute() method executes the INSERT statement in the flightQuery string.
A
  1. The commit() method saves the insert in the database before the cursor is released.
77
Q
A
78
Q
  1. The user enters flight data.
  2. flightQuery contains three %s placeholders. Values are assigned when the cursor executes.
  3. flightData tuple contains three values.
A
  1. When the cursor executes, flightData values are assigned to placeholders. Python data types are automatically converted to MySQL data types.
  2. commit() saves the insert in the database, and close() releases the cursor resources.
79
Q
A
80
Q
  1. flightCursor executes a SELECT query.
  2. fetchall() returns a set of tuples. Each tuple is a row of the result table.
  3. The for statement processes each tuple in the set. row[0] contains data from the first column, and row[1] from the second column.
A
  1. flightCursor executes a SELECT query.
  2. fetchall() returns a set of tuples. Each tuple is a row of the result table.
  3. The for statement processes each tuple in the set. row[0] contains data from the first column, and row[1] from the second column.
81
Q
A
82
Q
  1. FlightCount counts the number of flights for an airline.
  2. flightData contains an airline name and a 0 placeholder for the quantity parameter.
  3. callproc() calls FlightCount with the input tuple flightData and returns the output tuple to result.
  4. result[1] is the second entry of the result tuple.
A
  1. FlightCount counts the number of flights for an airline.
  2. flightData contains an airline name and a 0 placeholder for the quantity parameter.
  3. callproc() calls FlightCount with the input tuple flightData and returns the output tuple to result.
  4. result[1] is the second entry of the result tuple.
83
Q
A
84
Q
A

Expected:
(A) connect
(B) cursor
(C) rowcursor
(D) query
(E) fetchall
(F) close
(G) close

(A) connect: Establishes a connection, providing login and database information as parameters.

(B) cursor: Creates a cursor for the connection.

(C), (D) rowcursor.execute(query): Executes query and makes results available to the cursor.

(E) fetchall: Retrieves all results from the cursor in one function call.

(F) rowcursor.close(): Closes the cursor. A cursor must be released with the close() method prior to closing the associated connection.

(G) connection.close(): Closes the connection.

85
Q
A

Expected:
(A) connect
(B) cursor
(C) execute
(D) query
(E) fetchall
(F) close
(G) connection

(A) connect: Establishes a connection, providing login and database information as parameters.

(B) cursor: Creates a cursor for the connection.

(C), (D) rowcursor.execute(query): Executes query and makes results available to the cursor.

(E) fetchall: Retrieves all results from the cursor in one function call.

(F) rowcursor.close(): Closes the cursor. A cursor must be released with the close() method prior to closing the associated connection.

(G) connection.close(): Closes the connection.

86
Q
  1. The web browser sends a request to the web server for a PHP script called db.php. The web server executes the PHP script.
  2. The DSN indicates that PDO should connect to a MySQL server running on localhost (127.0.0.1) and use the database named “Reservation”.
  3. The PDO constructor uses the DSN and MySQL username and password to create a database connection.
A
  1. If the connection succeeds, the PHP script uses the PDO object to execute queries.
  2. If the connection fails, the PDOException produces a fatal error.
  3. If PHP is configured to show fatal errors, the web server responds with a web page showing the error details, which is then displayed by the web browser.
87
Q
A
88
Q
  1. The PDO object creates a connection to MySQL.
  2. setAttribute() makes PDO throw a PDOException if an error occurs executing an SQL query.
  3. The query() method executes the INSERT statement in the $sql string. The flight is inserted into the Flight table.
A
  1. MySQL server returns information to the web server indicating the result of the INSERT statement. The information is returned from query() as a PDOStatement.
  2. The rowCount() method returns 1 because one row was inserted into the Flight table.
89
Q
A
90
Q
  1. A user enters flight information into a form. The form submits to addflight.php, and submitted values are placed in the $_POST array.
  2. The INSERT statement uses ? as placeholders for three values.
A
  1. perpare() prepares the parameterized statement and returns a PDOStatement.
  2. bindValue() binds the values from $_POST to the parameter identifiers.
  3. execute() executes the INSERT statement. addflight.php displays a confirmation message.
91
Q
A
92
Q
  1. The prepared statement is executed(), selecting China Airlines flights out of PEK.
  2. fetch() returns an array containing a single row from the results table.
  3. The while loop processes each row until all rows are processed. Result values are accessed from $row using the column names.
A
  1. The prepared statement is executed(), selecting China Airlines flights out of PEK.
  2. fetch() returns an array containing a single row from the results table.
  3. The while loop processes each row until all rows are processed. Result values are accessed from $row using the column names.
93
Q
A
94
Q
  1. FlightCount counts the number of flights for an airline.
  2. The prepared statement uses a CALL statement to call FlightCount with the airline “China Airlines”.
A
  1. execute() calls FlightCount. The OUT parameter quantity is assigned to the @count variable.
  2. A second query fetches the value of @count.
95
Q
A