home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _e2bc085e20e9b5b2b55b654803043e33 < prev    next >
Text File  |  2000-03-23  |  25KB  |  561 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>DBD::CSV - DBI driver for CSV files</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> DBD::CSV - DBI driver for CSV files</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#warning">WARNING</A></LI>
  25.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  26.     <UL>
  27.  
  28.         <LI><A HREF="#prerequisites">Prerequisites</A></LI>
  29.         <LI><A HREF="#installation">Installation</A></LI>
  30.         <LI><A HREF="#creating a database handle">Creating a database handle</A></LI>
  31.         <LI><A HREF="#creating and dropping tables">Creating and dropping tables</A></LI>
  32.         <LI><A HREF="#inserting, fetching and modifying data">Inserting, fetching and modifying data</A></LI>
  33.         <LI><A HREF="#error handling">Error handling</A></LI>
  34.         <LI><A HREF="#metadata">Metadata</A></LI>
  35.         <LI><A HREF="#driver private methods">Driver private methods</A></LI>
  36.         <LI><A HREF="#data restrictions">Data restrictions</A></LI>
  37.     </UL>
  38.  
  39.     <LI><A HREF="#todo">TODO</A></LI>
  40.     <LI><A HREF="#known bugs">KNOWN BUGS</A></LI>
  41.     <LI><A HREF="#author and copyright">AUTHOR AND COPYRIGHT</A></LI>
  42.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  43. </UL>
  44. <!-- INDEX END -->
  45.  
  46. <HR>
  47. <P>
  48. <H1><A NAME="name">NAME</A></H1>
  49. <P>DBD::CSV - DBI driver for CSV files</P>
  50. <P>
  51. <HR>
  52. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  53. <UL>
  54. <LI>Linux</LI>
  55. <LI>Solaris</LI>
  56. <LI>Windows</LI>
  57. </UL>
  58. <HR>
  59. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  60. <PRE>
  61.     use DBI;
  62.     $dbh = DBI->connect("DBI:CSV:f_dir=/home/joe/csvdb")
  63.         or die "Cannot connect: " . $DBI::errstr;
  64.     $sth = $dbh->prepare("CREATE TABLE a (id INTEGER, name CHAR(10))")
  65.         or die "Cannot prepare: " . $dbh->errstr();
  66.     $sth->execute() or die "Cannot execute: " . $sth->errstr();
  67.     $sth->finish();
  68.     $dbh->disconnect();</PRE>
  69. <PRE>
  70.     # Read a CSV file with ";" as the separator, as exported by
  71.     # MS Excel. Note we need to escape the ";", otherwise it
  72.     # would be treated as an attribute separator.
  73.     $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\;});
  74.     $sth = $dbh->prepare("SELECT * FROM info");</PRE>
  75. <PRE>
  76.     # Same example, this time reading "info.csv" as a table:
  77.     $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\;});
  78.     $dbh->{'csv_tables'}->{'info'} = { 'file' => 'csv'};
  79.     $sth = $dbh->prepare("SELECT * FROM info");</PRE>
  80. <P>
  81. <HR>
  82. <H1><A NAME="warning">WARNING</A></H1>
  83. <P>THIS IS ALPHA SOFTWARE. It is *only* 'Alpha' because the interface (API)
  84. is not finalized. The Alpha status does not reflect code quality or
  85. stability.</P>
  86. <P>
  87. <HR>
  88. <H1><A NAME="description">DESCRIPTION</A></H1>
  89. <P>The DBD::CSV module is yet another driver for the DBI (Database independent
  90. interface for Perl). This one is based on the SQL ``engine'' SQL::Statement
  91. and the abstract DBI driver DBD::File and implements access to
  92. so-called CSV files (Comma separated values). Such files are mostly used for
  93. exporting MS Access and MS Excel data.</P>
  94. <P>See <A HREF="#item_DBI">DBI(3)</A> for details on DBI, <A HREF="../../../SQL/Statement(3).html">the SQL::Statement(3) manpage</A> for details on
  95. SQL::Statement and <A HREF="../../../DBD/File(3).html">the DBD::File(3) manpage</A> for details on the base class
  96. DBD::File.</P>
  97. <P>
  98. <H2><A NAME="prerequisites">Prerequisites</A></H2>
  99. <P>The only system dependent feature that DBD::File uses, is the <A HREF="../../../lib/Pod/perlfunc.html#item_flock"><CODE>flock()</CODE></A>
  100. function. Thus the module should run (in theory) on any system with
  101. a working <A HREF="../../../lib/Pod/perlfunc.html#item_flock"><CODE>flock()</CODE></A>, in particular on all Unix machines and on Windows
  102. NT. Under Windows 95 and MacOS the use of <A HREF="../../../lib/Pod/perlfunc.html#item_flock"><CODE>flock()</CODE></A> is disabled, thus
  103. the module should still be usable,</P>
  104. <P>Unlike other DBI drivers, you don't need an external SQL engine
  105. or a running server. All you need are the following Perl modules,
  106. available from any CPAN mirror, for example</P>
  107. <PRE>
  108.   <A HREF="ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/by-module">ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/by-module</A></PRE>
  109. <DL>
  110. <DT><STRONG><A NAME="item_DBI">DBI</A></STRONG><BR>
  111. <DD>
  112. the DBI (Database independent interface for Perl), version 1.00 or
  113. a later release
  114. <P></P>
  115. <DT><STRONG><A NAME="item_SQL%3A%3AStatement">SQL::Statement</A></STRONG><BR>
  116. <DD>
  117. a simple SQL engine
  118. <P></P>
  119. <DT><STRONG><A NAME="item_Text%3A%3ACSV_XS">Text::CSV_XS</A></STRONG><BR>
  120. <DD>
  121. this module is used for writing rows to or reading rows from CSV files.
  122. <P></P></DL>
  123. <P>
  124. <H2><A NAME="installation">Installation</A></H2>
  125. <P>Installing this module (and the prerequisites from above) is quite simple.
  126. You just fetch the archive, extract it with</P>
  127. <PRE>
  128.     gzip -cd DBD-CSV-0.1000.tar.gz | tar xf -</PRE>
  129. <P>(this is for Unix users, Windows users would prefer WinZip or something
  130. similar) and then enter the following:</P>
  131. <PRE>
  132.     cd DBD-CSV-0.1000
  133.     perl Makefile.PL
  134.     make
  135.     make test</PRE>
  136. <P>If any tests fail, let me know. Otherwise go on with</P>
  137. <PRE>
  138.     make install</PRE>
  139. <P>Note that you almost definitely need root or administrator permissions.
  140. If you don't have them, read the ExtUtils::MakeMaker man page for details
  141. on installing in your own directories. <A HREF="../../../lib/ExtUtils/MakeMaker.html">the ExtUtils::MakeMaker manpage</A>.</P>
  142. <P>
  143. <H2><A NAME="creating a database handle">Creating a database handle</A></H2>
  144. <P>Creating a database handle usually implies connecting to a database server.
  145. Thus this command reads</P>
  146. <PRE>
  147.     use DBI;
  148.     my $dbh = DBI->connect("DBI:CSV:f_dir=$dir");</PRE>
  149. <P>The directory tells the driver where it should create or open tables
  150. (a.k.a. files). It defaults to the current directory, thus the following
  151. are equivalent:</P>
  152. <PRE>
  153.     $dbh = DBI->connect("DBI:CSV:");
  154.     $dbh = DBI->connect("DBI:CSV:f_dir=.");</PRE>
  155. <P>You may set other attributes in the DSN string, separated by semicolons.</P>
  156. <P>
  157. <H2><A NAME="creating and dropping tables">Creating and dropping tables</A></H2>
  158. <P>You can create and drop tables with commands like the following:</P>
  159. <PRE>
  160.     $dbh->do("CREATE TABLE $table (id INTEGER, name CHAR(64))");
  161.     $dbh->do("DROP TABLE $table");</PRE>
  162. <P>Note that currently only the column names will be stored and no other data.
  163. Thus all other information including column type (INTEGER or CHAR(x), for
  164. example), column attributes (NOT NULL, PRIMARY KEY, ...) will silently be
  165. discarded. This may change in a later release.</P>
  166. <P>A drop just removes the file without any warning.</P>
  167. <P>See <A HREF="#item_DBI">DBI(3)</A> for more details.</P>
  168. <P>Table names cannot be arbitrary, due to restrictions of the SQL syntax.
  169. I recommend that table names are valid SQL identifiers: The first
  170. character is alphabetic, followed by an arbitrary number of alphanumeric
  171. characters. If you want to use other files, the file names must start
  172. with '/', './' or '../' and they must not contain white space.</P>
  173. <P>
  174. <H2><A NAME="inserting, fetching and modifying data">Inserting, fetching and modifying data</A></H2>
  175. <P>The following examples insert some data in a table and fetch it back:
  176. First all data in the string:</P>
  177. <PRE>
  178.     $dbh->do("INSERT INTO $table VALUES (1, "
  179.              . $dbh->quote("foobar") . ")");</PRE>
  180. <P>Note the use of the quote method for escaping the word 'foobar'. Any
  181. string must be escaped, even if it doesn't contain binary data.</P>
  182. <P>Next an example using parameters:</P>
  183. <PRE>
  184.     $dbh->do("INSERT INTO $table VALUES (?, ?)", undef,
  185.              2, "It's a string!");</PRE>
  186. <P>Note that you don't need to use the quote method here, this is done
  187. automatically for you. This version is particularly well designed for
  188. loops. Whenever performance is an issue, I recommend using this method.</P>
  189. <P>You might wonder about the <A HREF="../../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A>. Don't wonder, just take it as it
  190. is. :-) It's an attribute argument that I have never ever used and
  191. will be parsed to the prepare method as a second argument.</P>
  192. <P>To retrieve data, you can use the following:</P>
  193. <PRE>
  194.     my($query) = "SELECT * FROM $table WHERE id > 1 ORDER BY id";
  195.     my($sth) = $dbh->prepare($query);
  196.     $sth->execute();
  197.     while (my $row = $sth->fetchrow_hashref) {
  198.         print("Found result row: id = ", $row->{'id'},
  199.               ", name = ", $row->{'name'});
  200.     }
  201.     $sth->finish();</PRE>
  202. <P>Again, column binding works: The same example again.</P>
  203. <PRE>
  204.     my($query) = "SELECT * FROM $table WHERE id > 1 ORDER BY id";
  205.     my($sth) = $dbh->prepare($query);
  206.     $sth->execute();
  207.     my($id, $name);
  208.     $sth->bind_columns(undef, \$id, \$name);
  209.     while ($sth->fetch) {
  210.         print("Found result row: id = $id, name = $name\n");
  211.     }
  212.     $sth->finish();</PRE>
  213. <P>Of course you can even use input parameters. Here's the same example
  214. for the third time:</P>
  215. <PRE>
  216.     my($query) = "SELECT * FROM $table WHERE id = ?";
  217.     my($sth) = $dbh->prepare($query);
  218.     $sth->bind_columns(undef, \$id, \$name);
  219.     for (my($i) = 1;  $i <= 2;   $i++) {
  220.         $sth->execute($id);
  221.         if ($sth->fetch) {
  222.             print("Found result row: id = $id, name = $name\n");
  223.         }
  224.         $sth->finish();
  225.     }</PRE>
  226. <P>See <A HREF="#item_DBI">DBI(3)</A> for details on these methods. See <A HREF="../../../SQL/Statement(3).html">the SQL::Statement(3) manpage</A> for
  227. details on the WHERE clause.</P>
  228. <P>Data rows are modified with the UPDATE statement:</P>
  229. <PRE>
  230.     $dbh->do("UPDATE $table SET id = 3 WHERE id = 1");</PRE>
  231. <P>Likewise you use the DELETE statement for removing rows:</P>
  232. <PRE>
  233.     $dbh->do("DELETE FROM $table WHERE id > 1");</PRE>
  234. <P>
  235. <H2><A NAME="error handling">Error handling</A></H2>
  236. <P>In the above examples we have never cared about return codes. Of course,
  237. this cannot be recommended. Instead we should have written (for example):</P>
  238. <PRE>
  239.     my($query) = "SELECT * FROM $table WHERE id = ?";
  240.     my($sth) = $dbh->prepare($query)
  241.         or die "prepare: " . $dbh->errstr();
  242.     $sth->bind_columns(undef, \$id, \$name)
  243.         or die "bind_columns: " . $dbh->errstr();
  244.     for (my($i) = 1;  $i <= 2;   $i++) {
  245.         $sth->execute($id)
  246.             or die "execute: " . $dbh->errstr();
  247.         if ($sth->fetch) {
  248.             print("Found result row: id = $id, name = $name\n");
  249.         }
  250.     }
  251.     $sth->finish($id)
  252.         or die "finish: " . $dbh->errstr();</PRE>
  253. <P>Obviously this is tedious. Fortunately we have DBI's <EM>RaiseError</EM>
  254. attribute:</P>
  255. <PRE>
  256.     $dbh->{'RaiseError'} = 1;
  257.     $@ = '';
  258.     eval {
  259.         my($query) = "SELECT * FROM $table WHERE id = ?";
  260.         my($sth) = $dbh->prepare($query);
  261.         $sth->bind_columns(undef, \$id, \$name);
  262.         for (my($i) = 1;  $i <= 2;   $i++) {
  263.             $sth->execute($id);
  264.             if ($sth->fetch) {
  265.                 print("Found result row: id = $id, name = $name\n");
  266.             }
  267.         }
  268.         $sth->finish($id);
  269.     };
  270.     if ($@) { die "SQL database error: $@"; }</PRE>
  271. <P>This is not only shorter, it even works when using DBI methods within
  272. subroutines.</P>
  273. <P>
  274. <H2><A NAME="metadata">Metadata</A></H2>
  275. <P>The following attributes are handled by DBI itself and not by DBD::File,
  276. thus they all work as expected:</P>
  277. <PRE>
  278.     Active
  279.     ActiveKids
  280.     CachedKids
  281.     CompatMode             (Not used)
  282.     InactiveDestroy
  283.     Kids
  284.     PrintError
  285.     RaiseError
  286.     Warn                   (Not used)</PRE>
  287. <P>The following DBI attributes are handled by DBD::File:</P>
  288. <DL>
  289. <DT><STRONG><A NAME="item_AutoCommit">AutoCommit</A></STRONG><BR>
  290. <DD>
  291. Always on
  292. <P></P>
  293. <DT><STRONG><A NAME="item_ChopBlanks">ChopBlanks</A></STRONG><BR>
  294. <DD>
  295. Works
  296. <P></P>
  297. <DT><STRONG><A NAME="item_NUM_OF_FIELDS">NUM_OF_FIELDS</A></STRONG><BR>
  298. <DD>
  299. Valid after <CODE>$sth->execute</CODE>
  300. <P></P>
  301. <DT><STRONG><A NAME="item_NUM_OF_PARAMS">NUM_OF_PARAMS</A></STRONG><BR>
  302. <DD>
  303. Valid after <CODE>$sth->prepare</CODE>
  304. <P></P>
  305. <DT><STRONG><A NAME="item_NAME">NAME</A></STRONG><BR>
  306. <DD>
  307. Valid after <CODE>$sth->execute</CODE>; undef for Non-Select statements.
  308. <P></P>
  309. <DT><STRONG><A NAME="item_NULLABLE">NULLABLE</A></STRONG><BR>
  310. <DD>
  311. Not really working. Always returns an array ref of one's, as DBD::CSV
  312. doesn't verify input data. Valid after <CODE>$sth->execute</CODE>; undef for
  313. non-Select statements.
  314. <P></P></DL>
  315. <P>These attributes and methods are not supported:</P>
  316. <PRE>
  317.     bind_param_inout
  318.     CursorName
  319.     LongReadLen
  320.     LongTruncOk</PRE>
  321. <P>In addition to the DBI attributes, you can use the following dbh
  322. attributes:</P>
  323. <DL>
  324. <DT><STRONG><A NAME="item_f_dir">f_dir</A></STRONG><BR>
  325. <DD>
  326. This attribute is used for setting the directory where CSV files are
  327. opened. Usually you set it in the dbh, it defaults to the current
  328. directory (``.''). However, it is overwritable in the statement handles.
  329. <P></P>
  330. <DT><STRONG><A NAME="item_csv_eol">csv_eol</A></STRONG><BR>
  331. <DD>
  332. <DT><STRONG><A NAME="item_csv_sep_char">csv_sep_char</A></STRONG><BR>
  333. <DD>
  334. <DT><STRONG><A NAME="item_csv_quote_char">csv_quote_char</A></STRONG><BR>
  335. <DD>
  336. <DT><STRONG><A NAME="item_csv_escape_char">csv_escape_char</A></STRONG><BR>
  337. <DD>
  338. <DT><STRONG><A NAME="item_csv_class">csv_class</A></STRONG><BR>
  339. <DD>
  340. <DT><STRONG><A NAME="item_csv_csv">csv_csv</A></STRONG><BR>
  341. <DD>
  342. The attributes <EM>csv_eol</EM>, <EM>csv_sep_char</EM>, <EM>csv_quote_char</EM> and
  343. <EM>csv_escape_char</EM> are corresponding to the respective attributes of the
  344. Text::CSV_XS object. You want to set these attributes if you have unusual
  345. CSV files like <EM>/etc/passwd</EM> or MS Excel generated CSV files with a semicolon
  346. as separator. Defaults are ``\015\012'', ';', '``' and '''', respectively.
  347. <P>The attributes are used to create an instance of the class <EM>csv_class</EM>,
  348. by default Text::CSV_XS. Alternatively you may pass an instance as
  349. <EM>csv_csv</EM>, the latter takes precedence. Note that the <EM>binary</EM>
  350. attribute <EM>must</EM> be set to a true value in that case.</P>
  351. <P>Additionally you may overwrite these attributes on a per-table base in
  352. the <EM>csv_tables</EM> attribute.</P>
  353. <P></P>
  354. <DT><STRONG><A NAME="item_csv_tables">csv_tables</A></STRONG><BR>
  355. <DD>
  356. This hash ref is used for storing table dependent metadata. For any
  357. table it contains an element with the table name as key and another
  358. hash ref with the following attributes:
  359. <DL>
  360. <DT><STRONG><A NAME="item_file">file</A></STRONG><BR>
  361. <DD>
  362. The tables file name; defaults to
  363. <PRE>
  364.     "$dbh->{f_dir}/$table"</PRE>
  365. <P></P>
  366. <DT><STRONG><A NAME="item_eol">eol</A></STRONG><BR>
  367. <DD>
  368. <DT><STRONG><A NAME="item_sep_char">sep_char</A></STRONG><BR>
  369. <DD>
  370. <DT><STRONG><A NAME="item_quote_char">quote_char</A></STRONG><BR>
  371. <DD>
  372. <DT><STRONG><A NAME="item_escape_char">escape_char</A></STRONG><BR>
  373. <DD>
  374. <DT><STRONG><A NAME="item_class">class</A></STRONG><BR>
  375. <DD>
  376. <DT><STRONG><A NAME="item_csv">csv</A></STRONG><BR>
  377. <DD>
  378. These correspond to the attributes <EM>csv_eol</EM>, <EM>csv_sep_char</EM>,
  379. <EM>csv_quote_char</EM>, <EM>csv_escape_char</EM>, <EM>csv_class</EM> and <EM>csv_csv</EM>.
  380. The difference is that they work on a per-table base.
  381. <P></P>
  382. <DT><STRONG><A NAME="item_col_names">col_names</A></STRONG><BR>
  383. <DD>
  384. <DT><STRONG><A NAME="item_skip_first_row">skip_first_row</A></STRONG><BR>
  385. <DD>
  386. By default DBD::CSV assumes that column names are stored in the first
  387. row of the CSV file. If this is not the case, you can supply an array
  388. ref of table names with the <EM>col_names</EM> attribute. In that case the
  389. attribute <EM>skip_first_row</EM> will be set to FALSE.
  390. <P>If you supply an empty array ref, the driver will read the first row
  391. for you, count the number of columns and create column names like
  392. <CODE>col0</CODE>, <CODE>col1</CODE>, ...</P>
  393. <P></P></DL>
  394. </DL>
  395. <P>Example: Suggest you want to use <EM>/etc/passwd</EM> as a CSV file. :-)
  396. There simplest way is:</P>
  397. <PRE>
  398.     require DBI;
  399.     my $dbh = DBI->connect("DBI:CSV:f_dir=/etc;csv_eol=\n;"
  400.                            . "csv_sep_char=:;csv_quote_char=;"
  401.                            . "csv_escape_char=");
  402.     $dbh->{'csv_tables'}->{'passwd'} = {
  403.         'col_names' => ["login", "password", "uid", "gid", "realname",
  404.                         "directory", "shell"]
  405.     };
  406.     $sth = $dbh->prepare("SELECT * FROM passwd");</PRE>
  407. <P>Another possibility where you leave all the defaults as they are and
  408. overwrite them on a per table base:</P>
  409. <PRE>
  410.     require DBI;
  411.     my $dbh = DBI->connect("DBI:CSV:");
  412.     $dbh->{'csv_tables'}->{'passwd'} = {
  413.         'eol' => "\n",
  414.         'sep_char' => ":",
  415.         'quote_char' => undef,
  416.         'escape_char' => undef,
  417.         'file' => '/etc/passwd',
  418.         'col_names' => ["login", "password", "uid", "gid", "realname",
  419.                         "directory", "shell"]
  420.     };
  421.     $sth = $dbh->prepare("SELECT * FROM passwd");</PRE>
  422. <P>
  423. <H2><A NAME="driver private methods">Driver private methods</A></H2>
  424. <P>These methods are inherited from DBD::File:</P>
  425. <DL>
  426. <DT><STRONG><A NAME="item_data_sources">data_sources</A></STRONG><BR>
  427. <DD>
  428. The <A HREF="#item_data_sources"><CODE>data_sources</CODE></A> method returns a list of subdirectories of the current
  429. directory in the form ``DBI:CSV:directory=$dirname''.
  430. <P>If you want to read the subdirectories of another directory, use</P>
  431. <PRE>
  432.     my($drh) = DBI->install_driver("CSV");
  433.     my(@list) = $drh->data_sources('f_dir' => '/usr/local/csv_data' );</PRE>
  434. <P></P>
  435. <DT><STRONG><A NAME="item_list_tables">list_tables</A></STRONG><BR>
  436. <DD>
  437. This method returns a list of file names inside $dbh->{'directory'}.
  438. Example:
  439. <PRE>
  440.     my($dbh) = DBI->connect("DBI:CSV:directory=/usr/local/csv_data");
  441.     my(@list) = $dbh->func('list_tables');</PRE>
  442. <P>Note that the list includes all files contained in the directory, even
  443. those that have non-valid table names, from the view of SQL. See
  444. <A HREF="#creating and dropping tables">Creating and dropping tables</A> above.</P>
  445. <P></P></DL>
  446. <P>
  447. <H2><A NAME="data restrictions">Data restrictions</A></H2>
  448. <P>When inserting and fetching data, you will sometimes be surprised: DBD::CSV
  449. doesn't correctly handle data types, in particular NULLs. If you insert
  450. integers, it might happen, that fetch returns a string. Of course, a string
  451. containing the integer, so that's perhaps not a real problem. But the
  452. following will never work:</P>
  453. <PRE>
  454.     $dbh->do("INSERT INTO $table (id, name) VALUES (?, ?)",
  455.              undef, "foo bar");
  456.     $sth = $dbh->prepare("SELECT * FROM $table WHERE id IS NULL");
  457.     $sth->execute();
  458.     my($id, $name);
  459.     $sth->bind_columns(undef, \$id, \$name);
  460.     while ($sth->fetch) {
  461.         printf("Found result row: id = %s, name = %s\n",
  462.               defined($id) ? $id : "NULL",
  463.               defined($name) ? $name : "NULL");
  464.     }
  465.     $sth->finish();</PRE>
  466. <P>The row we have just inserted, will never be returned! The reason is
  467. obvious, if you examine the CSV file: The corresponding row looks
  468. like</P>
  469. <PRE>
  470.     "","foo bar"</PRE>
  471. <P>In other words, not a NULL is stored, but an empty string. CSV files
  472. don't have a concept of NULL values. Surprisingly the above example
  473. works, if you insert a NULL value for the name! Again, you find
  474. the explanation by examining the CSV file:</P>
  475. <PRE>
  476.     ""</PRE>
  477. <P>In other words, DBD::CSV has ``emulated'' a NULL value by writing a row
  478. with less columns. Of course this works only if the rightmost column
  479. is NULL, the two rightmost columns are NULL, ..., but the leftmost
  480. column will never be NULL!</P>
  481. <P>See <A HREF="#creating and dropping tables">Creating and dropping tables</A> above for table name restrictions.</P>
  482. <P>
  483. <HR>
  484. <H1><A NAME="todo">TODO</A></H1>
  485. <P>Extensions of DBD::CSV:</P>
  486. <DL>
  487. <DT><STRONG><A NAME="item_CSV_file_scanner">CSV file scanner</A></STRONG><BR>
  488. <DD>
  489. Write a simple CSV file scanner that reads a CSV file and attempts
  490. to guess sep_char, quote_char, escape_char and eol automatically.
  491. <P></P></DL>
  492. <P>These are merely restrictions of the DBD::File or SQL::Statement
  493. modules:</P>
  494. <DL>
  495. <DT><STRONG><A NAME="item_Joins">Joins</A></STRONG><BR>
  496. <DD>
  497. The current version of the module works with single table SELECTs
  498. only, although the basic design of the SQL::Statement module allows
  499. joins and the like.
  500. <P></P>
  501. <DT><STRONG><A NAME="item_Table_name_mapping">Table name mapping</A></STRONG><BR>
  502. <DD>
  503. Currently it is not possible to use files with names like <CODE>names.csv</CODE>.
  504. Instead you have to use soft links or rename files. As an alternative
  505. one might use, for example a dbh attribute 'table_map'. It might be a
  506. hash ref, the keys being the table names and the values being the file
  507. names.
  508. <P></P>
  509. <DT><STRONG><A NAME="item_Column_name_mapping">Column name mapping</A></STRONG><BR>
  510. <DD>
  511. Currently the module assumes that column names are stored in the first
  512. row. While this is fine in most cases, there should be a possibility
  513. of setting column names and column number from the programmer: For
  514. example MS Access doesn't export column names by default.
  515. <P></P></DL>
  516. <P>
  517. <HR>
  518. <H1><A NAME="known bugs">KNOWN BUGS</A></H1>
  519. <UL>
  520. <LI>
  521. The module is using <A HREF="../../../lib/Pod/perlfunc.html#item_flock"><CODE>flock()</CODE></A> internally. However, this function is not
  522. available on platforms. Using <A HREF="../../../lib/Pod/perlfunc.html#item_flock"><CODE>flock()</CODE></A> is disabled on MacOS and Windows
  523. 95: There's no locking at all (perhaps not so important on these
  524. operating systems, as they are for single users anyways).
  525. <P></P></UL>
  526. <P>
  527. <HR>
  528. <H1><A NAME="author and copyright">AUTHOR AND COPYRIGHT</A></H1>
  529. <P>This module is Copyright (C) 1998 by</P>
  530. <PRE>
  531.     Jochen Wiedmann
  532.     Am Eisteich 9
  533.     72555 Metzingen
  534.     Germany</PRE>
  535. <PRE>
  536.     Email: joe@ispsoft.de
  537.     Phone: +49 7123 14887</PRE>
  538. <P>All rights reserved.</P>
  539. <P>You may distribute this module under the terms of either the GNU
  540. General Public License or the Artistic License, as specified in
  541. the Perl README file.</P>
  542. <P>
  543. <HR>
  544. <H1><A NAME="see also">SEE ALSO</A></H1>
  545. <P><A HREF="#item_DBI">DBI(3)</A>, <A HREF="../../../Text/CSV_XS(3).html">the Text::CSV_XS(3) manpage</A>, <A HREF="../../../SQL/Statement(3).html">the SQL::Statement(3) manpage</A></P>
  546. <P>For help on the use of DBD::CSV, see the DBI users mailing list:</P>
  547. <PRE>
  548.   <A HREF="http://www.isc.org/dbi-lists.html">http://www.isc.org/dbi-lists.html</A></PRE>
  549. <P>For general information on DBI see</P>
  550. <PRE>
  551.   <A HREF="http://www.symbolstone.org/technology/perl/DBI">http://www.symbolstone.org/technology/perl/DBI</A></PRE>
  552. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  553. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  554. <STRONG><P CLASS=block> DBD::CSV - DBI driver for CSV files</P></STRONG>
  555. </TD></TR>
  556. </TABLE>
  557.  
  558. </BODY>
  559.  
  560. </HTML>
  561.