dbm.html

来自「perl教程」· HTML 代码 · 共 390 行 · 第 1/4 页

HTML
390 行
字号
<p>Will create a file that has a structure something like this:</p>
<pre>
  _metadata \0 | foo,bar
  zippy        | 1</pre>
<p>The next time you access this table with DBD::DBM, it will treat the _metadata row as a header rather than as data and will pull the column names from there.  However, if you access the file with something other than DBD::DBM, the row will be treated as a regular data row.</p>
<p>If you do not want the column names stored as a data row in the table you can set the <em>dbm_store_metadata</em> attribute to 0.</p>
<pre>
 <span class="keyword">my</span> <span class="variable">$dbh</span> <span class="operator">=</span> <span class="variable">DBI</span><span class="operator">-&gt;</span><span class="keyword">connect</span><span class="operator">(</span><span class="string">'dbi:DBM:store_metadata=0'</span><span class="operator">);</span>
</pre>
<p>or</p>
<pre>
 <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">dbm_store_metadata</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>or, for per-table setting</p>
<pre>
 <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">dbm_tables</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">qux</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">store_metadata</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>By default, DBD::DBM assumes that you have two columns named &quot;k&quot; and &quot;v&quot; (short for &quot;key&quot; and &quot;value&quot;).  So if you have <em>dbm_store_metadata</em> set to 1 and you want to use alternate column names, you need to specify the column names like this:</p>
<pre>
 <span class="keyword">my</span> <span class="variable">$dbh</span> <span class="operator">=</span> <span class="variable">DBI</span><span class="operator">-&gt;</span><span class="keyword">connect</span><span class="operator">(</span><span class="string">'dbi:DBM:store_metadata=0;cols=foo,bar'</span><span class="operator">);</span>
</pre>
<p>or</p>
<pre>
 <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">dbm_store_metadata</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
 <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">dbm_cols</span><span class="operator">}</span>           <span class="operator">=</span> <span class="string">'foo,bar'</span><span class="operator">;</span>
</pre>
<p>To set the column names on per-table basis, do this:</p>
<pre>
 <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">dbm_tables</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">qux</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">store_metadata</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
 <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">dbm_tables</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">qux</span><span class="operator">}</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">cols</span><span class="operator">}</span>           <span class="operator">=</span> <span class="string">'foo,bar'</span><span class="operator">;</span>
 <span class="comment">#</span>
 <span class="comment"># sets the column names only for table "qux"</span>
</pre>
<p>If you have a file that was created by another DBM program or created with <em>dbm_store_metadata</em> set to zero and you want to convert it to using DBD::DBM's column name storage, just use one of the methods above to name the columns but *without* specifying <em>dbm_store_metadata</em> as zero.  You only have to do that once - thereafter you can get by without setting either <em>dbm_store_metadata</em> or setting <em>dbm_cols</em> because the names will be stored in the file.</p>
<p>
</p>
<h2><a name="statement_handle___sth__attributes_and_methods">Statement handle ($sth) attributes and methods</a></h2>
<p>Most statement handle attributes such as NAME, NUM_OF_FIELDS, etc. are available only after an execute.  The same is true of $sth-&gt;rows which is available after the execute but does <em>not</em> require a fetch.</p>
<p>
</p>
<h2><a name="the__dbh_dbm_versions___method">The $dbh-&gt;<code>dbm_versions()</code> method</a></h2>
<p>The private method <code>dbm_versions()</code> presents a summary of what other modules are being used at any given time.  DBD::DBM can work with or without many other modules - it can use either SQL::Statement or DBI::SQL::Nano as its SQL engine, it can be run with DBI or DBI::PurePerl, it can use many kinds of DBM modules, and many kinds of serializers when run with MLDBM.  The <code>dbm_versions()</code> method reports on all of that and more.</p>
<pre>
  <span class="keyword">print</span> <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="variable">dbm_versions</span><span class="operator">;</span>               <span class="comment"># displays global settings</span>
  <span class="keyword">print</span> <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="variable">dbm_versions</span><span class="operator">(</span><span class="variable">$table_name</span><span class="operator">);</span>  <span class="comment"># displays per table settings</span>
</pre>
<p>An important thing to note about this method is that when called with no arguments, it displays the *global* settings.  If you over-ride these by setting per-table attributes, these will <em>not</em> be shown unless you specifiy a table name as an argument to the method call.</p>
<p>
</p>
<h2><a name="storing_objects">Storing Objects</a></h2>
<p>If you are using MLDBM, you can use DBD::DBM to take advantage of its serializing abilities to serialize any Perl object that MLDBM can handle.  To store objects in columns, you should (but don't absolutely need to) declare it as a column of type BLOB (the type is *currently* ignored by the SQL engine, but heh, it's good form).</p>
<p>You *must* use placeholders to insert or refer to the data.</p>
<p>
</p>
<hr />
<h1><a name="gotchas_and_warnings">GOTCHAS AND WARNINGS</a></h1>
<p>Using the SQL DROP command will remove any file that has the name specified in the command with either '.pag' or '.dir' or your {dbm_ext} appended to it.  So
this be dangerous if you aren't sure what file it refers to:</p>
<pre>
 <span class="variable">$dbh</span><span class="operator">-&gt;</span><span class="keyword">do</span><span class="operator">(</span><span class="string">qq{DROP TABLE "/path/to/any/file"}</span><span class="operator">);</span>
</pre>
<p>Each DBM type has limitations.  SDBM_File, for example, can only store values of less than 1,000 characters.  *You* as the script author must ensure that you don't exceed those bounds.  If you try to insert a value that is bigger than the DBM can store, the results will be unpredictable.  See the documentation for whatever DBM you are using for details.</p>
<p>Different DBM implementations return records in different orders.  That means that you can <em>not</em> depend on the order of records unless you use an ORDER BY statement.  DBI::SQL::Nano does not currently support ORDER BY (though it may soon) so if you need ordering, you'll have to install SQL::Statement.</p>
<p>DBM data files are platform-specific.  To move them from one platform to another, you'll need to do something along the lines of dumping your data to CSV on platform #1 and then dumping from CSV to DBM on platform #2.  DBD::AnyData and DBD::CSV can help with that.  There may also be DBM conversion tools for your platforms which would probably be quickest.</p>
<p>When using MLDBM, there is a very powerful serializer - it will allow you to store Perl code or objects in database columns.  When these get de-serialized, they may be evaled - in other words MLDBM (or actually Data::Dumper when used by MLDBM) may take the values and try to execute them in Perl.  Obviously, this can present dangers, so if you don't know what's in a file, be careful before you access it with MLDBM turned on!</p>
<p>See the entire section on <a href="#table_locking_and_flock__">Table locking and flock()</a> for gotchas and warnings about the use of flock().</p>
<p>
</p>
<hr />
<h1><a name="getting_help__making_suggestions__and_reporting_bugs">GETTING HELP, MAKING SUGGESTIONS, AND REPORTING BUGS</a></h1>
<p>If you need help installing or using DBD::DBM, please write to the DBI users mailing list at <a href="mailto:dbi-users@perl.org">dbi-users@perl.org</a> or to the comp.lang.perl.modules newsgroup on usenet.  I'm afraid I can't always answer these kinds of questions quickly and there are many on the mailing list or in the newsgroup who can.</p>
<p>If you have suggestions, ideas for improvements, or bugs to report, please write me directly at the email shown below.</p>
<p>When reporting bugs, please send the output of $dbh-&gt;<code>dbm_versions($table)</code> for a table that exhibits the bug and, if possible, as small a sample as you can make of the code that produces the bug.  And of course, patches are welcome too :-).</p>
<p>
</p>
<hr />
<h1><a name="acknowledgements">ACKNOWLEDGEMENTS</a></h1>
<p>Many, many thanks to Tim Bunce for prodding me to write this, and for copious, wise, and patient suggestions all along the way.</p>
<p>
</p>
<hr />
<h1><a name="author_and_copyright">AUTHOR AND COPYRIGHT</a></h1>
<p>This module is written and maintained by</p>
<p>Jeff Zucker &lt; jzucker AT cpan.org &gt;</p>
<p>Copyright (c) 2004 by Jeff Zucker, all rights reserved.</p>
<p>You may freely distribute and/or modify this module under the terms of either the GNU General Public License (GPL) or the Artistic License, as specified in the Perl README file.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/DBI.html">the DBI manpage</a>, <a href="../../SQL/Statement.html">the SQL::Statement manpage</a>, <a href="../../lib/DBI/SQL/Nano.html">the DBI::SQL::Nano manpage</a>, <a href="../../lib/AnyDBM_File.html">the AnyDBM_File manpage</a>, <em>MLDBM</em></p>

</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?