22
PHP Aufbaukurs Tag 2. PHP5 & SQLite

sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

PHPAufbaukurs

Tag 2. PHP5 & SQLite

Page 2: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

2

PHP Aufbaukurs 19.09.2006 | Folie 2 Doz. Dr. –Ing. Igor Olkhovskiy

Organisatorisches

Igor Olkhovskiy

Dr. Dipl.- Ing.

Kontakt: [email protected]

Page 3: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

3

PHP Aufbaukurs 19.09.2006 | Folie 3 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite

• In C geschriebene, in eigene Anwendung (einbettbare Datenbank).• Unterstützt einen großteil des SQL:1992 Standards.• Kombination von Datenbank und Schnittstelle in einer einzigen Bibliothek.

Page 4: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

4

PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy

PHP & SQLite

• PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung einerDatenbankschnittstelle in PHP.

- für PHP 4 in PECL.- ab PHP 5 Bestandteil der Standarddistribution und standardmäßig

aktiviert.• Prozedurale und objektorientierte Schnittstelle.• Unterstützung für das Iterator-Interface von PHP 5.

Page 5: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

5

PHP Aufbaukurs 19.09.2006 | Folie 5 Doz. Dr. –Ing. Igor Olkhovskiy

Tag 2. PHP-Aufbau. SQLite

Plan

• Konfiguration von Apache, PHP5 und SQLite

• SQLite Datenbank erzeugen (Übung1)

• SQLite Datenbank abfragen

• Konfiguration von SQLite – ODBC

• SQLite Datenbank über ODBC (Übung1,2,3)

Page 6: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

6

PHP Aufbaukurs 19.09.2006 | Folie 6 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite-Commander. Erste Tabelle

Laden Sie den sqlite-commander (sqlite.exe) hierunterErzeugen Sie Ihre erste SQLite - Datenbank z.B. so:

Datenbankname

Tabellestruktur NeueDaten

Abfrage und Ergebnis

Page 7: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

7

PHP Aufbaukurs 19.09.2006 | Folie 7 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. Installation

Führen Sie die Testdatei mit der Funktion phpinfo() aus.Kontrollieren Sie ob die SQLite - Datenbank schon vorinstalliert ist.

Page 8: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

8

PHP Aufbaukurs 19.09.2006 | Folie 8 Doz. Dr. –Ing. Igor Olkhovskiy

PHP. Erste SQLite-tabelle in Browser auslesen. Direkte Verbindung

Jetzt können Sie folgendes PHP-programm aufschreiben:<?php// Neue Datenbank schaffen$db = sqlite_open("C:\Webroot\sqlite\Example\db\mydb");

// SQL-Abfrage$sqlite = "SELECT * FROM Tabelle1;";$result = sqlite_query($db, $sqlite);$rows = array();

//Headerecho "<h3>SQLite-Abfrage:<h3><h3>".$sqlite."</h3>";echo "<table border = 1><th>&nbsp;f1&nbsp;</th><th>&nbsp;f2&nbsp;</th><th>&nbsp;f3&nbsp;</th>";

//Daten auslesenwhile ($rows = sqlite_fetch_array($result)){

echo "<tr><td>".$rows['f1']."</td><td>&nbsp;".$rows['f2']."</td><td>&nbsp;".$rows['f3']."</td></tr>";}echo "</table>";

sqlite_close($db);?>

Page 9: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

9

PHP Aufbaukurs 19.09.2006 | Folie 9 Doz. Dr. –Ing. Igor Olkhovskiy

PHP. Erste SQLite-tabelle in Browser auslesen. Direkte Verbindung .Ergebnis

Ihr Ergebnis muss z.B. so aussehen:

Page 10: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

10

PHP Aufbaukurs 19.09.2006 | Folie 10 Doz. Dr. –Ing. Igor Olkhovskiy

PHP.SQLite. Direkte Verbindung . Neue Datenbank

//Eine neue SQLite - Datenbank definieren$db = sqlite_open("kundendb.sqlite");

Page 11: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

11

PHP Aufbaukurs 19.09.2006 | Folie 11 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. Direkte Verbindung. Übung 1.

sqlite_query($db,"CREATE TABLE Kundenliste

(Kundennummer INTEGER PRIMARY KEY,Anrede CHAR(50),Vorname CHAR(50),Nachname CHAR(50),Strasse CHAR(50),Postleitzahl CHAR(50),Ort CHAR(50),Telefon CHAR(50),eMail CHAR(50),Geburtstag CHAR(20));“

);

Page 12: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

12

PHP Aufbaukurs 19.09.2006 | Folie 12 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. Direkte Verbindung. Datei: dbconnect.php

$command ="INSERT INTO Kundenliste

(Kundennummer,Anrede,Vorname,Nachname,Strasse,Postleitzahl,Ort,Telefon,eMail,Geburtstag)

VALUES('1', 'Herr', 'Rolf', 'Bergmann'‚ 'Waldstrasse 1'‚'30169‚

'Hannover', '[email protected]‚ '01-04-1985');";

sqlite_query($db,$command);

Page 13: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

13

PHP Aufbaukurs 19.09.2006 | Folie 13 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. Select

$sqlite = "SELECT * FROM Kundenliste;";$result = sqlite_query($db, $sqlite);

//Daten auslesen$rows = array();while ($rows = sqlite_fetch_array($result))

echo $rows['Kundennummer'].$rows['Anrede'].$rows['Vorname'].$rows['Nachname'].$rows['Strasse'].$rows['Postleitzahl'].$rows['Ort'].$rows['Telefon'].$rows['eMail'].$rows['Geburtstag']."</ br>";

Page 14: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

14

PHP Aufbaukurs 19.09.2006 | Folie 14 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. Select. Beispiel

Page 15: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

15

PHP Aufbaukurs 19.09.2006 | Folie 15 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. ODBC - Verbindung. sqliteodbc-win32-0.64. Installation

Inst.exe ausführen

Page 16: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

16

PHP Aufbaukurs 19.09.2006 | Folie 16 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite .ODBC - Verbindung

Start->Einstellungen->Systemsteuerung->Verwaltung->Datenquellen(ODBC)

Page 17: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

17

PHP Aufbaukurs 19.09.2006 | Folie 17 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. ODBC - Verbindung. Übung1

<?php//ODBC-verbindung//$dsn="SQLiteDSN";$user="";$pwd="";

if (!$cc=odbc_connect($dsn, $user, $pwd)){echo "ODBC-Verbindung scheitert";exit;}

elseecho "<h1>ODBC-connection ist Ok!</h1>";

?>

Page 18: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

18

PHP Aufbaukurs 19.09.2006 | Folie 18 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. ODBC - Verbindung. Übung2

$table = "Kundenliste";

if (!$cc=odbc_connect($dsn, $user, $pwd)){echo "ODBC-Verbindung scheitert"; exit;}

else{ $sql = "SELECT * FROM ".$table;

$result = odbc_exec($cc, $sql);

odbc_fetch_row($result);echo "<div align=\"center\"><br><h2>SQLite Tabelle:".$table.

"</h2><table border = 2 cellpadding = 5>";echo "<th>Fieldname</th><th>Type</th><th>Gro?e</th>";

for ($col=1; $col<=odbc_num_fields($result); $col++){$field = odbc_field_name($result, $col);

$type = odbc_field_type($result, $col);

$len = odbc_field_len($result, $col);echo "<tr>";

echo "<td><font size='4' color=\"red\">".$field."</font></td>";echo "<td>".$type."</td><td>".$len."</td>";

echo "</tr>";}

echo "</table></div>";}

Page 19: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

19

PHP Aufbaukurs 19.09.2006 | Folie 19 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. ODBC - Verbindung. Übung2. Ergebnis

Page 20: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

20

PHP Aufbaukurs 19.09.2006 | Folie 20 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. ODBC - Verbindung. Übung3. Struktur

$table = "Kundenliste"; $sql = "SELECT * FROM ".$table;

$result = odbc_exec($cc, $sql);echo "<div align=\"center\"><h2>Abfrage: SELECT * FROM<b></b> $table</h2></div>";echo "<table border = 1>";//TableHeader

for ($col=1; $col<=odbc_num_fields($result); $col++) {$name = odbc_field_name($result, $col);

echo "<th bgcolor = #dddddd> ".$name."</th>"; }// Tabelleninhaltwhile (odbc_fetch_row($result))

{echo "<tr>";for ($col=1; $col<=odbc_num_fields($result); $col++) {

$conname=odbc_result($result,odbc_field_name($result, $col));echo "<td> ".$conname."</td>"; }

echo "<tr>";}echo "<table>";

Page 21: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

21

PHP Aufbaukurs 19.09.2006 | Folie 21 Doz. Dr. –Ing. Igor Olkhovskiy

SQLite. ODBC - Verbindung. Übung3. Ergebnis

Page 22: sqlite install uebungen - LUIS · 4 PHP Aufbaukurs 19.09.2006 | Folie 4 Doz. Dr. –Ing. Igor Olkhovskiy PHP & SQLite • PHP-Erweiterung für SQLite ist die derzeit schnellste Implementierung

22

PHP Aufbaukurs 19.09.2006 | Folie 22 Doz. Dr. –Ing. Igor Olkhovskiy

PHP. SQLite. Aufgabe

Schreiben Sie ein PHP-Programm um einige Daten in der Tabelle Kundendaten zu ändern.(z.B. statt Sandra Sommer erstellen Sie Sandra Winter).Benutzen Sie den SQL-Befehl UPDATE.(UPDATE Kundenliste SET Nachname = ‘Winter‘ WHERE Kundennummer = 5;)Erstellen Sie bitte 2 Variante:

• Erste – über sqlite_open (Direkte Verbindund)• Zweite – über odbc_connect (ODBC - Verbindung)