Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Appending single record to CSV (or TDV, not too late to switch) - filling 13 fields from 13 files, one of which is split into array of 2 and I just need half of it...

by poj (Abbot)
on Jan 22, 2018 at 19:55 UTC ( [id://1207703]=note: print w/replies, xml ) Need Help??


in reply to Appending single record to CSV (or TDV, not too late to switch) - filling 13 fields from 13 files, one of which is split into array of 2 and I just need half of it...

and I'm thinking a CSV is good enough since I don't want to mess with SQL

Why not use both :) A simple demo for you

#!/usr/bin/perl use strict; use warnings; use DBD::CSV; # create db handle my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_ext => ".csv/r", RaiseError => 1, }) or die "Cannot connect: $DBI::errstr"; # create csv table if not exists my $table = 'mycache'; unless (-e $table.'.csv'){ $dbh->do ("CREATE TABLE $table ( uid char(100), line1 char(100), line2 char(100), line3 char(100), datetime char(20) ) "); } while (1) { # show records my $ar = $dbh->selectall_arrayref("SELECT uid FROM $table ORDER BY u +id"); print "$table.csv contains\n"; print " $_->[0]\n" for @$ar; print "\n"; # input new or existing record print "Input unique id (q to quit,c to clear) > "; chomp( my $input = <STDIN> ); exit if lc $input eq 'q'; # clear if (lc $input eq 'c'){ $dbh->do("DELETE FROM $table"); next; } # check existing or new my @f = $dbh->selectrow_array(" SELECT * FROM $table WHERE uid = ?",undef,$input); if (@f){ print "--\n $input EXISTS : @f \n--\n"; } else { my @f = ($input,'init1','init2','init3',scalar localtime); print "--\n $input NEW RECORD : @f \n--\n"; $dbh->do("INSERT INTO $table VALUES (?,?,?,?,?)",undef,@f); } }
poj
  • Comment on Re: Appending single record to CSV (or TDV, not too late to switch) - filling 13 fields from 13 files, one of which is split into array of 2 and I just need half of it...
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found