sqlite install uebungen - luis · 4 php aufbaukurs 19.09.2006 | folie 4 doz. dr. –ing. igor...
TRANSCRIPT
PHPAufbaukurs
Tag 2. PHP5 & SQLite
2
PHP Aufbaukurs 19.09.2006 | Folie 2 Doz. Dr. –Ing. Igor Olkhovskiy
Organisatorisches
Igor Olkhovskiy
Dr. Dipl.- Ing.
Kontakt: [email protected]
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.
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.
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)
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
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.
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> f1 </th><th> f2 </th><th> f3 </th>";
//Daten auslesenwhile ($rows = sqlite_fetch_array($result)){
echo "<tr><td>".$rows['f1']."</td><td> ".$rows['f2']."</td><td> ".$rows['f3']."</td></tr>";}echo "</table>";
sqlite_close($db);?>
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:
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");
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));“
);
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);
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>";
14
PHP Aufbaukurs 19.09.2006 | Folie 14 Doz. Dr. –Ing. Igor Olkhovskiy
SQLite. Select. Beispiel
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
16
PHP Aufbaukurs 19.09.2006 | Folie 16 Doz. Dr. –Ing. Igor Olkhovskiy
SQLite .ODBC - Verbindung
Start->Einstellungen->Systemsteuerung->Verwaltung->Datenquellen(ODBC)
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>";
?>
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>";}
19
PHP Aufbaukurs 19.09.2006 | Folie 19 Doz. Dr. –Ing. Igor Olkhovskiy
SQLite. ODBC - Verbindung. Übung2. Ergebnis
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>";
21
PHP Aufbaukurs 19.09.2006 | Folie 21 Doz. Dr. –Ing. Igor Olkhovskiy
SQLite. ODBC - Verbindung. Übung3. Ergebnis
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)