SQLite

Allgemein

SQLite (https://www.sqlite.org/ ) hat den Vorteil, daß die gesamte Datenbank in einer Datei steckt. Keine Installation auf dem Server ist notwendig, Backups sind triviales kopieren der Datei.

Nachteil ist, daß manche SQL Statements so nicht funktionieren wie inMySQL oder langsam sind. Vor allem sind das Joins.

UPDATE mit JOIN

UPDATE jobs LEFT JOIN customers...

funktioniert leider nicht. Alternativ kann man folgendes Konstrukt verwenden (hier um über den Namen die id zu holen und als Foreign Key zu setzen, z.B. nach einem Import):

UPDATE jobs SET fk_customer = (SELECT id FROM customers WHERE customers.title = jobs.customer_import_name);

Erstellt: 11/2015| Geändert: 11/2015

Verwendung in PHP

Hier einige Befehle für Einsatz in PHP.

Select

// open database
$this->database = new \SQLite3($filename);

// get rows
$rows = $this->database->query($sql);
if($rows) {
     // make assoc array
     while ($row = $rows->fetchArray(SQLITE3_ASSOC)) {
          $results[] = $row;
     }
 }

Insert

Make Insert statement

// make statement from comma seperated lists of fields and values
$implodeArrayKeys = implode(', ', $insertKeys);
$implodeArrayValues = implode(', ', $insertValues);
$sql = sprintf("INSERT INTO ".$this->table." (%s) VALUES(%s)", $implodeArrayKeys, $implodeArrayValues);   

Execute

// execute
$this->database->exec($sql);

// get inserted row
$id = $this->database->lastInsertRowID();

Update

Make Update statement

// make statement from array
foreach ($data as $key => $value) {
   $updates[] = "$key = '$value'";
}
$implodeArray = implode(', ', $updates);
$sql = sprintf("UPDATE ".$this->table." SET %s WHERE id='%s'", $implodeArray, $id);

Execute

// execute
$this->database->exec($sql);

Escape

$sort =  \SQLite3::escapeString($sort);

Erstellt: 11/2015| Geändert: 11/2015