T.G. Cornholio has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: DBI::Excel Help Needed
by JupiterCrash (Monk) on Nov 24, 2002 at 21:35 UTC
    In MS Access, you would be able to get around this problem with this SQL statement:
    SELECT [Tracking Nbr] FROM Kaiser_VRT_Extract_Sac
    Try using square brackets, it might work in Excel also. In general, I like to avoid column names with spaces for this reason.
      Jupiter,

      This didn't work for me, I tried a few permutations on my own:

      SELECT [Tracking Nbr] FROM Kaiser_VRT_Extract_Sac
      Gives:

      SQL ERROR: Bad table or column name '[Tracking Nbr]' has chars not alp +hanumeric or underscore!
      Similar errors occur for:

      '[Tracking Nbr]' ['Tracking Nbr'] "[Tracking Nbr]" ["Tracking Nbr"]
      I agree on the avoidance issue, but unfortunately I'm stuck with what I have. When I get connected to the "real" database there's probably not going to be a problem, but it would be nice to do the development in an isolated environment.

      Thanks for the suggestion, let me know if you think of anything else. Anyone know where to look for the answer?