Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| lehrkraefte:blc:informatik:efi-2023:datenbanken [2024/03/25 14:40] – [Restore/Copy] Ivo Blöchliger | lehrkraefte:blc:informatik:efi-2023:datenbanken [2024/04/01 18:55] (current) – [Weitere Infos] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Sqlite ====== | ||
| + | Einfache und doch performante Datenbank (Einsatz für kleinere Webseiten, Apps, Browser): https:// | ||
| + | Ein Vorteil ist, dass die Datenbank keinen Server braucht und in einer einzigen Datei gespeichert ist (im Gegensatz zu den meisten anderen Datenbanken). | ||
| + | |||
| + | ===== Spielwiese ===== | ||
| + | Verbindung auf dem ofi.tech-lab.ch Server: | ||
| + | <code bash> | ||
| + | ssh ef | ||
| + | cd sqlite-demo | ||
| + | sqlite3 demo.sqlite | ||
| + | </ | ||
| + | |||
| + | Default-Einstellungen in der Datei '' | ||
| + | <code bash> | ||
| + | cat ~/ | ||
| + | </ | ||
| + | enthält zur Zeit: | ||
| + | <code txt> | ||
| + | .headers on | ||
| + | .mode column | ||
| + | PRAGMA foreign_keys = ON; | ||
| + | </ | ||
| + | |||
| + | |||
| + | Sqlite-Befehle: | ||
| + | <code bash> | ||
| + | .schema | ||
| + | .quit | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Tabellen anlegen ==== | ||
| + | https:// | ||
| + | <code sql> | ||
| + | CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| + | username TEXT UNIQUE NOT NULL, | ||
| + | password TEXT NOT NULL); | ||
| + | CREATE TABLE score (id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| + | userid int not null, | ||
| + | zeit TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| + | score int, | ||
| + | FOREIGN KEY(userid) REFERENCES user(id)); | ||
| + | </ | ||
| + | ==== Daten einfügen ==== | ||
| + | |||
| + | <code sql> | ||
| + | INSERT INTO score (score, userid) values (42, 1); | ||
| + | </ | ||
| + | Wird eine nicht-existente '' | ||
| + | |||
| + | ==== Daten abfragen ==== | ||
| + | <code sql> | ||
| + | SELECT * FROM score; | ||
| + | SELECT * FROM score ORDER BY score DESC; | ||
| + | SELECT * FROM score order by score desc limit 3; | ||
| + | select username, score from user,score where user.id=score.userid order by score desc; | ||
| + | select username, score from user inner join score on user.id=score.userid order by score desc; | ||
| + | </ | ||
| + | |||
| + | Hinweis: '' | ||
| + | |||
| + | ==== Daten modifizieren ==== | ||
| + | https:// | ||
| + | <code sql> | ||
| + | UPDATE score set score=123 where id=18; | ||
| + | </ | ||
| + | |||
| + | ==== Daten löschen ==== | ||
| + | https:// | ||
| + | <code sql> | ||
| + | DELETE from score where id=18; | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Backup ==== | ||
| + | <code bash> | ||
| + | echo -e " | ||
| + | </ | ||
| + | |||
| + | ==== Restore/ | ||
| + | <code bash> | ||
| + | sqlite3 copy.sqlite < demo.dump | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Weitere Infos ===== | ||
| + | * [[lehrkraefte: | ||
| + | * https:// | ||