I am trying to insert values into the database, My database table is as shown
$stmt = qq(CREATE TABLE IF NOT EXISTS INFO
(
DeviceName TEXT NOT NULL,
CurrentStatus INT NOT NULL,
ReportTime TEXT,
OldStatus INT NOT NULL,
OldReportTime TEXT ););
Here before inserting to the db I am creating a unique index for DeviceName column, so that whenever a new entry comes into the db with same name it is not inserted rather gets replaced,
$stmt = qq(CREATE UNIQUE INDEX ree ON INFO(DeviceName));
$stmt = qq(INSERT OR REPLACE INTO INFO(DeviceName, CurrentStatus,ReportTime,OldStatus,OldReportTime)
VALUES ('$FQDN', $currentstatus, '$currenttime', $oldstatus, '$oldtime' ););
my $rv = $dbh->do($stmt) or die $DBI::errstr;
these are the few lines which i added to create a unique index and replace if the next entity enters with the same DeviceName. But though i followed this, the databases is still getting stored with the same DeviceName multiple times.
#!/usr/bin/perl
use DBI;
use strict;
my $FQDN= "bubbly.bth.se";
my $currenttime = "Sat Dec 9 02:07:31 2017";
my $oldtime = "Sat Dec 9 02:06:31 2017";
my $currentstatus = "1";
my $oldstatus = "2";
my $driver = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully\n";
$stmt = qq(CREATE TABLE IF NOT EXISTS INFO
(
DeviceName TEXT NOT NULL,
CurrentStatus INT NOT NULL,
ReportTime TEXT,
OldStatus INT NOT NULL,
OldReportTime TEXT ););
my $rv = $dbh->do($stmt);
if(my $rv < 0) {
print $DBI::errstr;
} else {
print "Table created successfully\n";
}
$stmt = qq(CREATE UNIQUE INDEX ree ON INFO(DeviceName));
$stmt = qq(INSERT OR REPLACE INTO INFO(DeviceName,
CurrentStatus,ReportTime,OldStatus,OldReportTime)
VALUES ('$FQDN', $currentstatus, '$currenttime', $oldstatus,
'$oldtime' ););
my $rv = $dbh->do($stmt) or die $DBI::errstr;
$dbh->disconnect();
NetSNMP::TrapReceiver::register("all", \&my_receiver) ||
warn "failed to register our perl trap handler\n";
print STDERR "Loaded the example perl snmptrapd handler\n";
this is the code i am using, my output
bubbly.bth.se|1|Sat Dec 9 02:07:31 2017|2|Sat Dec 9 02:06:31 2017
bubbly.bth.se|1|Sat Dec 9 02:07:31 2017|2|Sat Dec 9 02:06:31 2017
bubbly.bth.se|1|Sat Dec 9 02:07:31 2017|2|Sat Dec 9 02:06:31 2017
bubbly.bth.se|1|Sat Dec 9 02:07:31 2017|2|Sat Dec 9 02:06:31 2017
bubbly.bth.se|1|Sat Dec 9 02:07:31 2017|2|Sat Dec 9 02:06:31 2017
But i wanted to have only one row in the database which updates or replaces every time if it finds the same DeviceName.
NOTE: Variable values are taken just for easy understanding.
`