# Up above your main loop my $DBH; my $STH = setup_database(); while (1) { . . . SNIP . . . } $DBH->disconnect; sub setup_database { my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile" ,"",""); my $q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; return $dbh->prepare($q); } sub process_data { if ($debug) { say "Processing polled data...." } my $json_line = shift(@_); my $dt = DateTime->now(); $dt->set_time_zone( 'America/Chicago' ); if ( not exists($json->{'ra'}) ) { $json->{ra} = $json->{rr} = 'null'; } if ( not exists($json->{'bbl'}) ) { $json->{bbl} = $json->{irl} = $json->{lux} = 'null'; } $STH->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(), $json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'}, $json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'}, $json->{'lux'}, $json->{'bbl'}, $json->{'irl'}, $json->{'ra'}, $json->{'rr'} ); }