1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| use DBI; use strict;
my $driver = "SQLite"; my $db_name = "xmodulo.db"; my $dbd = "DBI:$driver:dbname=$db_name";
my $username = ""; my $password = "";
my $dbh = DBI->connect($dbd, $username, $password, { RaiseError => 1 }) or die $DBI::errstr; print STDERR "Database opened successfully\n";
my $stmt = qq(CREATE TABLE IF NOT EXISTS NETWORK (ID INTEGER PRIMARY KEY AUTOINCREMENT, HOSTNAME TEXT NOT NULL, IPADDRESS INT NOT NULL, OS CHAR(50), CPULOAD REAL);); my $ret = $dbh->do($stmt); if($ret < 0) { print STDERR $DBI::errstr; } else { print STDERR "Table created successfully\n"; }
$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD) VALUES ('xmodulo', 16843009, 'Ubuntu 14.10', 0.0)); $ret = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD) VALUES ('bert', 16843010, 'CentOS 7', 0.0)); $ret = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(INSERT INTO NETWORK (HOSTNAME,IPADDRESS,OS,CPULOAD) VALUES ('puppy', 16843011, 'Ubuntu 14.10', 0.0)); $ret = $dbh->do($stmt) or die $DBI::errstr;
$stmt = qq(SELECT id, hostname, os, cpuload from NETWORK;); my $obj = $dbh->prepare($stmt); $ret = $obj->execute() or die $DBI::errstr;
if($ret < 0) { print STDERR $DBI::errstr; } while(my @row = $obj->fetchrow_array()) { print "ID: ". $row[0] . "\n"; print "HOSTNAME: ". $row[1] ."\n"; print "OS: ". $row[2] ."\n"; print "CPULOAD: ". $row[3] ."\n\n"; }
$stmt = qq(UPDATE NETWORK set CPULOAD = 50 where OS='Ubuntu 14.10';); $ret = $dbh->do($stmt) or die $DBI::errstr;
if( $ret < 0 ) { print STDERR $DBI::errstr; } else { print STDERR "A total of $ret rows updated\n"; }
$stmt = qq(DELETE from NETWORK where ID=2;); $ret = $dbh->do($stmt) or die $DBI::errstr;
if($ret < 0) { print STDERR $DBI::errstr; } else { print STDERR "A total of $ret rows deleted\n"; }
$dbh->disconnect(); print STDERR "Exit the database\n";
|