From 165edd75bd35ff51756b715d3d853ff445a39596 Mon Sep 17 00:00:00 2001 From: MatMasIt Date: Mon, 7 Feb 2022 07:55:14 +0100 Subject: [PATCH] Added sql dump maker --- server/workbookExport.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/server/workbookExport.php b/server/workbookExport.php index 2d4b67d..269dd93 100644 --- a/server/workbookExport.php +++ b/server/workbookExport.php @@ -22,3 +22,31 @@ $writer->save("data/current.ods"); $spreadsheet->disconnectWorksheets(); unset($spreadsheet); + +$db = new SQLite3(dirname(__FILE__)."/db"); +$db->busyTimeout(5000); + +$sql=""; + +$tables=$db->query("SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%';"); + +while ($table=$tables->fetchArray(SQLITE3_NUM)) { + $sql.=$db->querySingle("SELECT sql FROM sqlite_master WHERE name = '{$table[0]}'").";\n\n"; + $rows=$db->query("SELECT * FROM {$table[0]}"); + $sql.="INSERT INTO {$table[0]} ("; + $columns=$db->query("PRAGMA table_info({$table[0]})"); + $fieldnames=array(); + while ($column=$columns->fetchArray(SQLITE3_ASSOC)) { + $fieldnames[]=$column["name"]; + } + $sql.=implode(",",$fieldnames).") VALUES"; + while ($row=$rows->fetchArray(SQLITE3_ASSOC)) { + foreach ($row as $k=>$v) { + $row[$k]="'".SQLite3::escapeString($v)."'"; + } + $sql.="\n(".implode(",",$row)."),"; + } + $sql=rtrim($sql,",").";\n\n"; +} + +file_put_contents("data/current.sql",$sql);