This looks so much like the synopsys of DBD::RAM that I had to try it (you need DBD::RAM and DBI installed) :

#!/usr/bin/perl -w use strict; use DBI; # create the data base handle my $dbh = DBI->connect('DBI:RAM:', {RaiseError=>1}); # load the table in memory $dbh->func({ table_name => 'members', # table name col_names => 'id, title, description', # column names data_type => 'PIPE', # pipe separated columns data_source => [<DATA>], # it could also be a fil +e }, 'import' ); # 2 ways to get the new id print "last id: ", get_last_id( $dbh), "\n"; print "first available id: ", get_first_available_id( $dbh), "\n"; # insert a new record my $new_id= get_last_id( $dbh) + 1; $dbh->do( qq[ INSERT INTO members (id, title, description) VALUES ($new_id, 'New Title', 'New Description')]); # output the result $dbh->func( { data_source => 'SELECT * FROM members', data_type => 'PIPE', data_target => 'toto.txt', }, 'export'); # DBD::RAM does not support the MAX function so we have to do it ourse +lves # just get the first id from the list of id's sorted in descending ord +er sub get_last_id { my( $dbh)= @_; return $dbh->selectcol_arrayref(qq[ SELECT id FROM members ORDER B +Y id DESC ])->[0]; } sub get_first_available_id { my( $dbh)= @_; # get the list of id's sorted by ascending order my $ids= $dbh->selectcol_arrayref(qq[ SELECT id FROM members ORDE +R BY id ASC ]); # go through the list until there is a hole my $id= 1; $id++ while( $id == shift @$ids); return $id; } __DATA__ 1|Title 1|Description 1 2|Title 2|Description 2 4|Title 3|Description 3

This way the day you decide to switch from a text DB to a real one you can just change import your data in the new DB, change the table creation function, remove the export et voila!


In reply to Re: Storing Info in a text File by mirod
in thread Storing Info in a text File by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.