150 likes | 289 Views
Database application development. INF08104: Database Systems Brian Davison , 2013/14. Agenda. Objects and relations Embedded SQL operations Interoperability. The role of databases. A specialist component in a ranger system Support for multiple concurrent users
E N D
Database application development INF08104: Database Systems Brian Davison, 2013/14
Agenda • Objects and relations • Embedded SQL operations • Interoperability
The role of databases • A specialist component in a ranger system • Support for multiple concurrent users • Fine-grained access rights model • Secure and reliable backup facilities • Portability across multiple platforms (DBMS and operating systems) http://netbeans.org
Mapping options • Single table Multiple tables • Issues • Many null values • Loss of generality
Restoring generality using views CREATE VIEW all_programmes AS SELECT title, null, null, nullFROM TV_programmeUNIONSELECT title, number_of_episodes, null, nullFROM seriesUNIONSELECT title, null, subject, nullFROM factualUNIONSELECT title, null, subject, periodFROM history_documentary
Embedded SQL • Typical process • Connect • Prepare statement • Execute statement • Loop over results • Close connection • Examples using PHP and MySQL
Database connection $db_host = "server_1";$username = "hr";$password = "hr";// Connect to MySQLmysql_connect($db_host, $username, $password);
Constructing and sending a query $query = "SELECT employee_id, first_name, last_name ";$query .= "FROM employees ";$query .= "WHERE department_id = " . $deptId . " ";$query .= "ORDER BY last_name";$result = mysql_query($query);
Processing query results echo "<table>";while ($loc = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $loc{employee_id} . "</td>"; echo "<td>" . $loc{first_name} . "</td>"; echo "<td>" . $loc{last_name} . "</td>"; echo "</tr>";}echo "</table>";
Issues • Null values • Error handling $result = mysql_query($query) or die("Unable to retrieve employee information: " . mysql_error());
DML $query = "UPDATE employees "; $query .= "SET commission_pct = " . $newCommission . " "; $query .= "WHERE employee_id = " . $empId; $result = mysql_query($query) or die("Unable to update commission: " . mysql_error()); echo mysql_affected_rows() . " row(s) updated";
Closing the database connection mysql_close();
Interoperability • ODBC • Connectivity for desktop tools • Practical exercise this week • XML