Code (PHP) pasted on 2019-01-13, 13:28 Raw Source
- <?php
- //$securePath = '../00_private';
- $db = new PDO('sqlite:' . $securePath . '/finances.sqlite3');
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- try {
- PRAGMA journal_mode = wal;
- PRAGMA foreign_keys = ON;
- ");
- CREATE TABLE IF NOT EXISTS account (
- name TEXT PRIMARY KEY
- );
- ");
- CREATE TABLE IF NOT EXISTS payer (
- name TEXT PRIMARY KEY
- );
- ");
- CREATE TABLE IF NOT EXISTS payment (
- account INTEGER,
- payer INTEGER,
- subject TEXT,
- date NUMERIC,
- amount NUMERIC,
- type TEXT,
- FOREIGN KEY(account) REFERENCES account(rowid) ON UPDATE CASCADE ON DELETE RESTRICT,
- FOREIGN KEY(payer) REFERENCES payer(rowid) ON UPDATE CASCADE ON DELETE RESTRICT
- );
- CREATE INDEX idx_payment_amount ON payment(date, amount);
- CREATE INDEX idx_payment_subject ON payment(date, subject);
- ");
- CREATE TABLE IF NOT EXISTS category (
- pid INTEGER,
- name TEXT PRIMARY KEY
- );
- ");
- CREATE TABLE IF NOT EXISTS payment2category (
- paymentID INTEGER,
- categoryID INTEGER,
- FOREIGN KEY(paymentID) REFERENCES payment(rowid) ON UPDATE CASCADE ON DELETE RESTRICT,
- FOREIGN KEY(categoryID) REFERENCES category(rowid) ON UPDATE CASCADE ON DELETE RESTRICT
- );
- CREATE INDEX idx_p2c_pc ON payment2category(paymentID, categoryID);
- CREATE INDEX idx_p2c_cp ON payment2category(categoryID, paymentID);
- ");
- } catch (PDOException $e) {
- echo $e->getMessage();
- }