Wise Monks, Please enlighten me regarding DBI:Excel and SQL. I have connected to an Excel spreadsheet using DBI:Excel, however some of the field names contain spaces. This poses a problem for me when I attempt to use one of these names in either the list of fields to select or the conditional statement.

For example:
SELECT 'Tracking Nbr' FROM Kaiser_VRT_Extract_Sac

Produces:
SQL ERROR: Bad table or column name '?0?' has chars not alphanumeric or underscore!

I have tried various combinations of quotes and symbols hoping to stumble on the solution using the patented "Blind Dumb Luck" method, to no avail. In the future, this will be connected to a "real" database, so I cannot use one of the other methods for interacting with Excel. I was hoping this would make it easy for me to develop then port to the database server when the time comes.
Here is my code:
use DBI; $debug = 1; $| = 1; my $kaiser_file = 'd:\perl\scripts\kevin\excel_data\query.xls'; my $amr_file = 'd:\perl\scripts\kevin\excel_data\data.xls'; $amr_db = DBI->connect("DBI:Excel:file=$amr_file") or die "Cannot connect: " . $DBI::errstr; print "Connected to: $amr_file\n"; foreach $table ($amr_db->tables()) { print "Table: $table "; $sth = $amr_db->prepare("SELECT * from $table LIMIT 2"); $sth->execute(); if ($sth->{NUM_OF_FIELDS} <= 1) { print "has $sth->{NUM_OF_FIELDS} fields. Skipping.\n"; next; } print "has $sth->{NUM_OF_FIELDS} fields. Using this table.\n"; $amr_table = $table; for ($i = 1 ; $i <= $sth->{NUM_OF_FIELDS} ; $i++) { $col_name = $sth->{NAME}->[$i-1]; printf(" Col %2d, Name = \'%s\'\n",$i,$col_name); $amr_col_num{$col_name} = $i; $amr_col_name{$i} = $col_name; } $sth->finish(); } $sql = "SELECT 'Tracking Nbr' FROM $amr_table"; print "$sql\n"; $amr_sth = $amr_db->prepare($sql); $amr_sth->execute(); $row_count = 1; while ($hashref = $amr_sth->fetchrow_hashref()) { print "Row: $row_count\n"; $row_count++; foreach $key (keys (%$hashref)) { print " $key => $$hashref{$key}\n"; } } $sth->finish(); if ($kaiser_db) { print "Disconnecting from kaiser.\n"; $kaiser_db->disconnect(); }
This produces the following output:
Connected to: d:\perl\scripts\kevin\excel_data\data.xls Table: Kaiser_VRT_Extract_Sac has 23 fields. Using this table. Col 1, Name = 'PupPriority' Col 2, Name = 'PupProblem' Col 3, Name = 'Referral Nbr' Col 4, Name = 'Tracking Nbr' Col 5, Name = 'IncidentNumber' Col 6, Name = 'TCR' Col 7, Name = 'PickupPromised' Col 8, Name = 'DTAlert' Col 9, Name = 'DTEnroute' Col 10, Name = 'DTAtScene' Col 11, Name = 'DTDepartScene' Col 12, Name = 'DTAtDestination' Col 13, Name = 'Cleared' Col 14, Name = 'PupLocName' Col 15, Name = 'PupAddress' Col 16, Name = 'PupZip' Col 17, Name = 'DestLocName' Col 18, Name = 'DestAddress' Col 19, Name = 'PatientName' Col 20, Name = 'CallerType' Col 21, Name = 'CancelReason' Col 22, Name = 'PupRoom' Col 23, Name = 'RunNumber' SELECT 'Tracking Nbr' FROM Kaiser_VRT_Extract_Sac SQL ERROR: Bad table or column name '?0?' has chars not alphanumeric o +r underscore! DBD::Excel::st fetchrow_hashref failed: Attempt to fetch row from a No +n-SELECT statement at test.pl line 35.
Your guidance is much appreciated

In reply to DBI::Excel Help Needed by T.G. Cornholio

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.