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->append_record("Caroline", "20", "sister");
$table->append_record_hash('jimmy', "age" => 25,
"msg" => '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' => 'Age' # index by the age, from young to old
'key' => '-Age' # index by the age, from old to young
'key' => '-Age(3)+Name' # index by the age(up to 999),then name; from old to young,then from A to Z
'key' => '-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->create_index( 'name' => 'indexA' ,
'key' => 'Age' , # '-Age' means reverse sort,
'permits'=> 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->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->prepare_select( ``seek'' => {'index'=> 'indexA',
'from' => 10,
'to' => 25},
``where'' => {'msg'=> 'hi'},
``top'' => 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'' => [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 "dbexample" or die DB_File::DB_Database->errstr;
my $table = new XBase "names.dbf" or die XBase->errstr;
# find people aged form 10 to 25, select the first 10 people. their 'msg' must content 'hi'
$table->prepare_select( "seek" => {'index'=> 'indexA',
'from' => 10,
'to' => 25},
"where" => {'msg'=> 'hi'},
"top" => 10);
while ( @_ = $table->get_record(undef,'age','msg') ){
### do something here
print ++$i,"\n";
print "@_ ","\n";
}</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 )