Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Perl DBI and MySQL Fieldname Problem
Hi,

I write:

# Get the field headers
# $sql = qq(select * from cartb;);
my $sth = &dbh -> prepare ($sql);
my @fields;
for my $field (@{$sth->{'NAME'}}) {
push @fields, $field;
}
$sth -> execute;

but i get a failure and the following error log:

[Tue Aug 03 20:20:59 2004] [error] 2928: ModPerl::Registry: Can't set
DBI::st=HASH(0x186b520)->{NAME}: unrecognised attribute or invalid value at
/projects/perl/importcaruk.com/modules/Car.pm line 86.

Surely this is textbook stuff... How come it won't let me grab the
fieldnames? Moving the $sth->execute doesn't help, and I know the database
connection is fine.

Any help appreciated.

Thanks,


Alex
http://www.alexite.com



Report this thread to moderator Post Follow-up to this message
Old Post
Alex
08-03-04 08:57 PM


Re: Perl DBI and MySQL Fieldname Problem
"Alex" <alex@alexbanks.com> wrote in message
news:410fe634$0$303$cc9e4d1f@news.dial.pipex.com...
> Hi,
>
> I write:
>
>  # Get the field headers
>  # $sql = qq(select * from cartb;);

For starters, a key line of code seems to be commented out.

>  my $sth = &dbh -> prepare ($sql);
>  my @fields;
>  for my $field (@{$sth->{'NAME'}}) {
>   push @fields, $field;
>  }
>  $sth -> execute;
>
> but i get a failure and the following error log:
>
> [Tue Aug 03 20:20:59 2004] [error] 2928: ModPerl::Registry: Can't set
> DBI::st=HASH(0x186b520)->{NAME}: unrecognised attribute or invalid value
at
> /projects/perl/importcaruk.com/modules/Car.pm line 86.
>
> Surely this is textbook stuff...

It is, literally. Read the documentation to DBI.pm. You have several
problems. You didn't just "miss something"

> How come it won't let me grab the
> fieldnames? Moving the $sth->execute doesn't help, and I know the database
> connection is fine.
>
> Any help appreciated.
>
> Thanks,
>
>
> Alex
> http://www.alexite.com
>
>




Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Palmer
08-04-04 08:55 AM


Re: Perl DBI and MySQL Fieldname Problem
"Alex" <alex@alexbanks.com> wrote in message
news:410fe634$0$303$cc9e4d1f@news.dial.pipex.com...
> Hi,
>
> I write:
>
>  # Get the field headers
>  # $sql = qq(select * from cartb;);

Helps to not comment out important pieces of code.

>  my $sth = &dbh -> prepare ($sql);

&dbh? The above code is not doing whatever you think it is doing...

>  my @fields;
>  for my $field (@{$sth->{'NAME'}}) {
>   push @fields, $field;
>  }
>  $sth -> execute;

You're now trying to access a result set *before* you execute the sql
statement?

>
> but i get a failure and the following error log:
>

I'm sure you'll get many other error messages as well. Personally, I would
write the above as follows: (untested)

my $sel_sth = $dbh->prepare("SELECT * FROM cartb");

$sel_sth->execute();

my $href = $sel_sth->fetchrow_hashref();

$sel_sth->finish();

foreach my $key (keys %$href) {
# each $key will be a column header
}

Matt



Report this thread to moderator Post Follow-up to this message
Old Post
Matt Garrish
08-04-04 08:56 PM


Re: Perl DBI and MySQL Fieldname Problem
"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
news:Pt5Qc.33103$Vm1.667649@news20.bellglobal.com...
>
> "Alex" <alex@alexbanks.com> wrote in message
> news:410fe634$0$303$cc9e4d1f@news.dial.pipex.com... 
>
> Helps to not comment out important pieces of code.
> 
>
> &dbh? The above code is not doing whatever you think it is doing...
> 
>
> You're now trying to access a result set *before* you execute the sql
> statement?
> 
>
> I'm sure you'll get many other error messages as well. Personally, I would
> write the above as follows: (untested)
>
> my $sel_sth = $dbh->prepare("SELECT * FROM cartb");
>
> $sel_sth->execute();
>
> my $href = $sel_sth->fetchrow_hashref();
>
> $sel_sth->finish();
>
> foreach my $key (keys %$href) {
>    # each $key will be a column header
> }

This code stops after fetching one row, no matter how many are returned by
the db query. The "fetchrow" part should go in a loop.

Second, there's (probably) no reason to fetch a hashref over an arrayref
(normally you know which fields you need, and don't ever "SELECT *"). A hash
is less efficient than an array, and the fields will come up out of order if
you loop over it. fetchrow_arrayref() is probably preferred.




Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Palmer
08-05-04 08:56 PM


Re: Perl DBI and MySQL Fieldname Problem
"Andrew Palmer" <andrewpalmer@email.com> wrote in message
news:NHsQc.2290$zc1.1213@fe40.usenetserver.com...
>
> "Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
> news:Pt5Qc.33103$Vm1.667649@news20.bellglobal.com... 
would 
>
> This code stops after fetching one row, no matter how many are returned by
> the db query. The "fetchrow" part should go in a loop.
>

No kidding? He asked how to get the column names. Why would you loop through
an entire result set when only one row is needed?

>
> Second, there's (probably) no reason to fetch a hashref over an arrayref
> (normally you know which fields you need, and don't ever "SELECT *"). A
hash
> is less efficient than an array, and the fields will come up out of order
if
> you loop over it. fetchrow_arrayref() is probably preferred.
>

I take it you haven't done much database work in your life. There are many
cases where you have no idea what table will be accessed or why. Try writing
a gui wrapper over a MySQL database. You won't have very meaningful data
sets without knowing what columns the data belong to.

Matt



Report this thread to moderator Post Follow-up to this message
Old Post
Matt Garrish
08-06-04 01:55 AM


Re: Perl DBI and MySQL Fieldname Problem
"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
news:8_yQc.42367$Vm1.953434@news20.bellglobal.com...
>
> "Andrew Palmer" <andrewpalmer@email.com> wrote in message
> news:NHsQc.2290$zc1.1213@fe40.usenetserver.com... 
> would 
by 
>
> No kidding? He asked how to get the column names.

Sorry, I lost track of what was going on. (Is "Alex" a he or a she?)

> Why would you loop through
> an entire result set when only one row is needed?
>

No rows are needed!!! You do not have to retrieve a row of data to get
column names (besides, there is no way to know the column order that way).
OP was trying to do it the correct way, with the "$sth->{'NAME'}" array
reference.

Also, I think your way could be truncated to something like:

my $href = $dbh->selectrow_hashref("SELECT * FROM cartb");

foreach my $key (keys %$href) {
# each $key will be a column header
}
 
> hash 
order
> if 
>
> I take it you haven't done much database work in your life. There are many
> cases where you have no idea what table will be accessed or why. Try
writing
> a gui wrapper over a MySQL database. You won't have very meaningful data
> sets without knowing what columns the data belong to.
>

Sheesh. I think we've hit everything. Try this:

# Get the field headers
$sql = qq(select * from cartb;);
my $sth = $dbh -> prepare ($sql);
$sth -> execute;
my @fields;
for my $field (@{$sth->{'NAME'}}) {
push @fields, $field;
}
$sth->finish();




Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Palmer
08-06-04 08:56 PM


Re: Perl DBI and MySQL Fieldname Problem
Or just (duh!)

my $sql = qq(select * from cartb;);
my $sth = $dbh -> prepare ($sql);
$sth -> execute;
my @fields=@{$sth->{'NAME'}};
$sth->finish();

If there really is a reason to copy that array, that is.




Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Palmer
08-07-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:40 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.