Clean the Database?

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Clean the Database?

Post by BarryT »

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!
Last edited by BarryT on Wednesday 19 October 2016 18:16, edited 2 times in total.
User avatar
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?

Post by Egregius »

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:

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 . ']');}
Gives output like this:

Code: Select all

2940 rows removed from Meter
4165 rows removed from Temperature
BarryT
Posts: 369
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: Clean the Database?

Post by BarryT »

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:

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 . ']');}
Gives output like this:

Code: Select all

2940 rows removed from Meter
4165 rows removed from Temperature
Hmm, i installed nginx with sqlite3...

I'm getting, Fatal error: Class 'SQLite3' not found in /var/www/html/clean.php on line 2
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Clean the Database?

Post by ben53252642 »

BarryT wrote: Wednesday 19 October 2016 18:23 I'm getting, Fatal error: Class 'SQLite3' not found in /var/www/html/clean.php on line 2
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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest