I’m trying to create a perl script which reads an exported Contacts file in CSV (comma-separated-values) format. I’m new to SQL and DBI and I’ve run into several problems:
1. I’m baffled by the CREATEd database column names. I used fname and lname in my test case. When I look at the successfully created database, I find the column names: FNAME and LNAME. Why uppercase? Furthermore, a SELECT would only work when I used the uppercase names. So, am I doing something wrong or is uppercase a requirement? The complete code follows:
2. I really need to use a name format with an embedded space, like “First Name”, but I cannot find the correct syntax. For example, single quotes result in the error:
But using:
(with escaped double quotes) didn’t result in an error but the column name had three pair of quotes:
So question 2, can a column name like ‘First Name’ be created using DBI with a CSV connect and if so how?
If you know of a specific tutorial that covers this specific use of perl DBI/CSV, please point me to it. I’ve read a lot of DBI stuff and found no solution. I couldn’t even get Martin Brown’s examples to work (from Perl! I Didn’t Know You Could Do That…” I’ve read the DBI tutorial on CPAN.
I’m running ActiveState Perl v5.6.1 and ppm installed the latest DBI. I’ve ppm verified my installation. I’m running Windows XP.
Thanks,
Gnubie
1. I’m baffled by the CREATEd database column names. I used fname and lname in my test case. When I look at the successfully created database, I find the column names: FNAME and LNAME. Why uppercase? Furthermore, a SELECT would only work when I used the uppercase names. So, am I doing something wrong or is uppercase a requirement? The complete code follows:
Code:
#!/usr/local/bin/perl -w
use DBI;
use strict;
my $createTable="CREATE TABLE contacts (fname Char, lname Char)";
my $db=DBI->connect("DBI:CSV:") or die "Cannot connect: ".$DBI::errstr;
$db->do($createTable) or die "Error: Couldn't CREATE TABLE, ".$db->errstr();
$db->disconnect();
exit(1);
1
2. I really need to use a name format with an embedded space, like “First Name”, but I cannot find the correct syntax. For example, single quotes result in the error:
Code:
SQL ERROR: Bad table or column name '?0?' has chars not alphanumeric or underscore!
Code:
my $createTable="CREATE TABLE contacts (‘First Name’ Char, lname Char)";
But using:
Code:
my $createTable="CREATE TABLE contacts (\"First Name\" Char, lname Char)";
(with escaped double quotes) didn’t result in an error but the column name had three pair of quotes:
Code:
"""FIRST NAME""",LNAME
So question 2, can a column name like ‘First Name’ be created using DBI with a CSV connect and if so how?
If you know of a specific tutorial that covers this specific use of perl DBI/CSV, please point me to it. I’ve read a lot of DBI stuff and found no solution. I couldn’t even get Martin Brown’s examples to work (from Perl! I Didn’t Know You Could Do That…” I’ve read the DBI tutorial on CPAN.
I’m running ActiveState Perl v5.6.1 and ppm installed the latest DBI. I’ve ppm verified my installation. I’m running Windows XP.
Thanks,
Gnubie