Hi,
please implend a "reset" button to clean all values/history of the database, without deleting the devices/hardware.
I just want to start over, for example the power usage and the gas, but it is not possible to just "reset" the database values without a sql manager..
We going moved over to another home in December, and as so far i get new (slim) meters, i just want to start over with the power usage, gas, to get a better monitor of the usage etc..
Would be great!
Clean the Database?
Moderators: leecollings, remb0
-
- Posts: 369
- Joined: Tuesday 31 March 2015 22:06
- Target OS: Linux
- Domoticz version: 2024.3
- Location: east netherlands
- Contact:
Clean the Database?
Last edited by BarryT on Wednesday 19 October 2016 18:16, edited 2 times in total.
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: Clean the Database?
Just use a sql manager. How many times will you need that?
If often, create some php page that deletes entrys from database, not that hard.
This script removes values from some tables for devices that aren't used or are older than one day:
Gives output like this:
If often, create some php page that deletes entrys from database, not that hard.
This script removes values from some tables for devices that aren't used or are older than one day:
Code: Select all
<?php
$db = new SQLite3('/volume1/@appstore/domoticz/var/domoticz.db');
$clean = strftime("%G-%m-%d %k:%M:%S",time()-86400);
$tables = array('LightingLog','Meter','Meter_Calendar','MultiMeter','MultiMeter_Calendar','Percentage','Percentage_Calendar','Rain','Rain_Calendar','Temperature','Temperature_Calendar','UV','UV_Calendar','Wind','Wind_Calendar');
foreach($tables as $table) {
$query=$db->exec("DELETE FROM $table WHERE DeviceRowID not in (select ID from DeviceStatus where Used = 1)");
if ($query) {
$rows = $db->changes();
if($rows>0) echo $rows." rows removed from $table<br/>";
}
$query=$db->exec("DELETE FROM $table WHERE Date < '$clean'");
if ($query) {
$rows=$db->changes();
if($rows>0) echo $rows." rows removed from $table<br/>";
}
}
$sql = 'VACUUM;';
if(!$result = $db->exec($sql)){ die('There was an error running the query [' . $db->error . ']');}
Code: Select all
2940 rows removed from Meter
4165 rows removed from Temperature
-
- Posts: 369
- Joined: Tuesday 31 March 2015 22:06
- Target OS: Linux
- Domoticz version: 2024.3
- Location: east netherlands
- Contact:
Re: Clean the Database?
Hmm, i installed nginx with sqlite3...Egregius wrote:Just use a sql manager. How many times will you need that?
If often, create some php page that deletes entrys from database, not that hard.
This script removes values from some tables for devices that aren't used or are older than one day:Gives output like this:Code: Select all
<?php $db = new SQLite3('/volume1/@appstore/domoticz/var/domoticz.db'); $clean = strftime("%G-%m-%d %k:%M:%S",time()-86400); $tables = array('LightingLog','Meter','Meter_Calendar','MultiMeter','MultiMeter_Calendar','Percentage','Percentage_Calendar','Rain','Rain_Calendar','Temperature','Temperature_Calendar','UV','UV_Calendar','Wind','Wind_Calendar'); foreach($tables as $table) { $query=$db->exec("DELETE FROM $table WHERE DeviceRowID not in (select ID from DeviceStatus where Used = 1)"); if ($query) { $rows = $db->changes(); if($rows>0) echo $rows." rows removed from $table<br/>"; } $query=$db->exec("DELETE FROM $table WHERE Date < '$clean'"); if ($query) { $rows=$db->changes(); if($rows>0) echo $rows." rows removed from $table<br/>"; } } $sql = 'VACUUM;'; if(!$result = $db->exec($sql)){ die('There was an error running the query [' . $db->error . ']');}
Code: Select all
2940 rows removed from Meter 4165 rows removed from Temperature
I'm getting, Fatal error: Class 'SQLite3' not found in /var/www/html/clean.php on line 2
-
- Posts: 543
- Joined: Saturday 02 July 2016 5:17
- Target OS: Linux
- Domoticz version: Beta
- Contact:
Re: Clean the Database?
I solved that error on my system with: apt-get install php-sqlite3
Egregius thanks for the script!
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Who is online
Users browsing this forum: Bing [Bot] and 1 guest