DB_File-DB_Database

 view release on metacpan or  search on metacpan

DB_Database.html  view on Meta::CPAN

<H2><A NAME="using it as table">Using it as Table</A></H2>
<DL>
<DT><STRONG>get_record</STRONG><BR>
<DD>
the same
<P></P>
<DT><STRONG>get_record_nf</STRONG><BR>
<DD>
the same
<P></P>
<DT><STRONG>get_record_hash</STRONG><BR>
<DD>
the same
<P></P></DL>
<P>
<H2><A NAME="writing the data">Writing the data</A></H2>
<P>Basically like above, but do not specify the ID, leave it to DB_File::DB_Database. 
The ID will be sequent numbers.
On success they return true -- the record ID. Index file is automatical updated, if needed.</P>
<DL>
<DT><STRONG>set_record</STRONG><BR>
<DD>
the same, recommand to use for updating data
<P></P>
<DT><STRONG>set_record_hash</STRONG><BR>
<DD>
the same, recommand to use for updating data
<P></P>
<DT><STRONG>update_record_hash</STRONG><BR>
<DD>
the same
<P></P>
<DT><STRONG>delete_record</STRONG><BR>
<DD>
the same
<P></P>
<DT><STRONG><A NAME="item_append_record">append_record</A></STRONG><BR>
<DD>
As parameters, takes the list of values of the fields. It append the record to the file. 
Unspecified fields (if you pass less than you should) are set to undef/empty. 
ID will be returned.
<P></P>
<DT><STRONG><A NAME="item_append_record_hash">append_record_hash</A></STRONG><BR>
<DD>
Unspecified fields (if you pass less than you should) are set to undef/empty. 
ID will be returned.
<P></P></DL>
<P>Examples:</P>
<PRE>
    $table-&gt;append_record(&quot;Caroline&quot;, &quot;20&quot;, &quot;sister&quot;);
    $table-&gt;append_record_hash('jimmy', &quot;age&quot; =&gt; 25,
                                        &quot;msg&quot; =&gt; 'Nothing is easy!');</PRE>
<P>
<H2><A NAME="using index">Using Index</A></H2>
<P>Index file is stored in DB_File BTREE. Once created, all index files will be automatically 
opened when open the database file, and updated automatically when writing the database file.</P>
<DL>
<DT><STRONG><A NAME="item_create_index">create_index</A></STRONG><BR>
<DD>
Create index file for one field. Default permits of the index file is 0640. 'name' is the index 
tag name, 'key' is the formula for indexing. For example:
<PRE>
  'key' =&gt; 'Age'            # index by the age, from young to old
  'key' =&gt; '-Age'           # index by the age, from old to young
  'key' =&gt; '-Age(3)+Name'   # index by the age(up to 999),then name; from old to young,then from A to Z
  'key' =&gt; '-Age(3)+-Name'  # index by the age(up to 999),then name; from old to young,then from Z to A</PRE>
<P>'Age(3)' is similar to 'substr(Age,0,3)', only the length of the last field name appeared in 
the 'key' can be ommited. '+-' CAN'T be subsituded by '-'.</P>
<PRE>
  # Index File name will be dbexample_indexA 
  print $table-&gt;create_index( 'name'   =&gt; 'indexA' ,
                              'key'    =&gt; 'Age' ,       # '-Age' means reverse sort,
                              'permits'=&gt; 0640 );</PRE>
<P></P>
<DT><STRONG><A NAME="item_recreate_index">recreate_index</A></STRONG><BR>
<DD>
Recreate the index file. Parameter is the index name(s).
<P></P>
<DT><STRONG><A NAME="item_drop_index">drop_index</A></STRONG><BR>
<DD>
Delete the index file. Parameter is the index name(s).
<PRE>
  # delete Index indexA 
  print $table-&gt;drop_index('indexA');</PRE>
<P></P></DL>
<P>
<H2><A NAME="select records">Select records</A></H2>
<P>Select matched records, using index will speed up the searching.</P>
<DL>
<DT><STRONG><A NAME="item_prepare_select">prepare_select</A></STRONG><BR>
<DD>
As parameters, pass a hash as parameters. Almost each value is a hash reference. Eg: 
  # find people aged form 10 to 25, select the first 10 people. their 'msg' must content 'hi'
  $table-&gt;prepare_select( ``seek''  =&gt; {'index'=&gt; 'indexA',
                                      'from' =&gt; 10,
                                      'to'   =&gt; 25},
                          ``where'' =&gt; {'msg'=&gt; 'hi'},
                          ``top''   =&gt; 10);
<P>If no ``seek'' specified(do not use index), it will search from the first record to the last(or up to the record numbers you needed).``top'' means select the first ? records. You may use ``cut'' instead, ``cut'' =&gt; [2,6] means select from the s...
<P>for ``seek'', ``from'' is needed, ``to'' can be omitted(till the last).</P>
<P>To fetch the selected record. Use get_record, get_record_nf, get_record_hash, leave the ID undef.</P>
<P></P></DL>
<P>Examples of selecting record:</P>
<PRE>
    use DB_File::DB_Database;
    my $table = new DB_File::DB_Database &quot;dbexample&quot; or die DB_File::DB_Database-&gt;errstr;
    my $table = new XBase &quot;names.dbf&quot; or die XBase-&gt;errstr;
    # find people aged form 10 to 25, select the first 10 people. their 'msg' must content 'hi'
    $table-&gt;prepare_select( &quot;seek&quot;  =&gt; {'index'=&gt; 'indexA',
                                        'from' =&gt; 10,
                                        'to'   =&gt; 25},
                            &quot;where&quot; =&gt; {'msg'=&gt; 'hi'},
                            &quot;top&quot;   =&gt; 10);
    while ( @_ = $table-&gt;get_record(undef,'age','msg') ){
         ### do something here
         print ++$i,&quot;\n&quot;;
         print &quot;@_ &quot;,&quot;\n&quot;;
    }</PRE>
<P>
<H2><A NAME="dumping the content of the file">Dumping the content of the file</A></H2>
<P>print the database file records and the index files contenting.</P>



( run in 0.834 second using v1.01-cache-2.11-cpan-13bb782fe5a )