Skip Menu |

This queue is for tickets about the SQL-Statement CPAN distribution.

Report information
The Basics
Id: 63072
Status: resolved
Priority: 0/
Queue: SQL-Statement

People
Owner: Nobody in particular
Requestors: jiaqiang.a.zhang [...] alcatel-sbell.com.cn
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: 1.32



Subject: error when using same table with different aliases in SQL query
Hello When I am using the same table (people.csv) with different aliases (h and w in my example) in one SQL query, I cannot get the correct result. Please help me with this. When I execute the following SQL : select c.husband,h.age,c.wife,w.age from couple c, people h, people w where c.husband = h.name and c.wife = w.name There is a error msg: DBD::CSV::st execute failed: Error 0 while reading file D:\Perl\people.csv: at C:/strawberry/perl/site/lib/SQL/Statement.pm line 813 [for Statement "select c.husband,h.age,c.wife,w.age from couple c, people h, people w where c.husband = h.name and c.wife = w.name"] at debug_dbd_csv.pl line 16. DBD::CSV::st fetchrow_hashref failed: Attempt to fetch row without a preceeding execute () call or from a non-SELECT statement [for Statement "select c.husband,h.age,c.wife,w.age from couple c, people h, people w where c.husband =h.name and c.wife = w.name"] at debug_dbd_csv.pl line 18. Or sometimes the following error msg: DBD::CSV::st execute failed: Error 2012 while reading file D:\Perl\people.csv: EOF - End of data in parsing input stream at C:/strawberry/perl/site/lib/SQL/Statement.pm line 813 [for Statement "select c.husband,h.age,c.wife,w.age from couple c, people h, people w where c.husband = h.name and c.wife = w.name"] at csv.pl line 99. Execute Err:Error 2012 while reading file D:\Perl\people.csv: EOF - End of datain parsing input stream at C:/strawberry/perl/site/lib/SQLStatement.pm line 813 What I am expected is the result as following: c.husband |h.age |c.wife |w.age | ================================================ James |30 |Mary |22 | Roger |24 |Lily |26 | ================================================ However, the following SQL queries work well: select * from couple; husband |wife | ======================== James |Mary | Roger |Lily | ======================== Return 2 columns and 2 lines select * from people; name |age | ======================== Mary |22 | Lily |26 | Roger |24 | James |30 | ======================== Return 2 columns and 4 lines select c.husband,h.age from couple c, people h where c.husband = h.name; c.husband |h.age | ======================== James |30 | Roger |24 | ======================== Return 2 columns and 2 lines select c.wife,w.age from couple c, people w where c.wife = w.name; c.wife |w.age | ======================== Mary |22 | Lily |26 | ======================== Return 2 columns and 2 lines I am using Strawberry perl 5.10.1 built for MSWin32-x86-multi-thread. The workaround would be that I duplicate the people.csv as people2.csv, and change my sql . There is the first time I report, so please tell me if there is anything missing.
Subject: couple.csv
Download couple.csv
application/octet-stream 38b

Message body not shown because it is not plain text.

Subject: debug_dbd_csv.pl
#!/usr/bin/perl -w use strict; use DBI; my $dbh = DBI->connect ("dbi:CSV:", "", "", { f_ext => ".csv", csv_null => 1, FetchHashKeyName => 'NAME_lc', }); my $query = "select c.husband,h.age,c.wife,w.age from couple c, people h, people w where c.husband = h.name and c.wife = w.name"; #my $query = "select c.husband,h.age from couple c, people h where c.husband = h.name "; my $sth = $dbh->prepare ($query); $sth->execute (); while (my $row = $sth->fetchrow_hashref) { foreach my $col ( keys %$row ) { print $col,"=",$row->{$col},"\t" if defined $row->{$col}; } print "done\n\n"; } $sth->finish ();
Subject: people.csv
Download people.csv
application/octet-stream 48b

Message body not shown because it is not plain text.

It's an SQL::Statement issue - moving to correct queue
Hi, I'm missing version information in your report, you reported DBD::CSV 0.31 only - neither DBI nor SQL::Statement. Assuming you're using SQL::Statement 1.31, the failiing line is Show quoted text
> TBLBFETCH: while ( my $array = $tableBobj->fetch_row($data) )
Regardless the version you're using, it's not supported (by design) to use the same table twice in a query. You can workaround by creating a temporary table so far: $dbh->do( "CREATE TEMP TABLE t_people AS SELECT * FROM people" ); $sth = $dbh->prepare( "select c.husband,h.age,c.wife,w.age from couple c, people h, t_people w where c.husband = h.name and c.wife = w.name" ); ... Best regards, Jens
CC: <Dan [...] DWright.Org>
Subject: RE: [rt.cpan.org #63072] error when using same table with different aliases in SQL query
Date: Wed, 17 Nov 2010 23:10:49 +0800
To: <bug-SQL-Statement [...] rt.cpan.org>
From: "ZHANG Jiaqiang A" <Jiaqiang.a.Zhang [...] alcatel-sbell.com.cn>
Hello Jens, Thanks for your reply. But to be honest, I don't understand why this report turn to SQL::Statement. Anyway, I suggest the documentation ( DBD::CSV or SQL::Statement ? I am not sure ) can warn the user for this restriction: "the same table cannot use twice in one query, even with different aliases" if it is designed in this way. Now I gave up CSV file and turn to use DBD::SQLite instead, it can support same table but with different aliases in one SQL. By the way, I am using DBI 1.615 and SQL::Statement 1.31. Best regards, Zhang Jiaqiang Show quoted text
-----Original Message----- From: Jens Rehsack via RT [mailto:bug-SQL-Statement@rt.cpan.org] Sent: 2010年11月17日 18:06 To: ZHANG Jiaqiang A Cc: Dan@DWright.Org Subject: [rt.cpan.org #63072] error when using same table with different aliases in SQL query <URL: https://rt.cpan.org/Ticket/Display.html?id=63072 > Hi, I'm missing version information in your report, you reported DBD::CSV 0.31 only - neither DBI nor SQL::Statement. Assuming you're using SQL::Statement 1.31, the failiing line is
> TBLBFETCH: while ( my $array = $tableBobj->fetch_row($data) )
Regardless the version you're using, it's not supported (by design) to use the same table twice in a query. You can workaround by creating a temporary table so far: $dbh->do( "CREATE TEMP TABLE t_people AS SELECT * FROM people" ); $sth = $dbh->prepare( "select c.husband,h.age,c.wife,w.age from couple c, people h, t_people w where c.husband = h.name and c.wife = w.name" ); ... Best regards, Jens
On Wed Nov 17 10:11:48 2010, zhangjiaqiang wrote: Show quoted text
> Hello Jens, > > Thanks for your reply. But to be honest, I don't understand why this > report turn to SQL::Statement.
Because DBD::CSV can't be blamed for SQL::Statement errors? Show quoted text
> Anyway, I suggest the documentation ( DBD::CSV or SQL::Statement ? I > am not sure ) can warn the user for this restriction: "the same > table cannot use twice in one query, even with different aliases" > if it is designed in this way.
Agreed. Limitations section should name it explicitely. Show quoted text
> Now I gave up CSV file and turn to use DBD::SQLite instead, it can > support same table but with different aliases in one SQL.
I know that the engine of SQLite is much better than SQL::Statement. As long as you don't need to deal with CSV or DBM files via SQL, you'd better avoid it when you expect full SQL support. Show quoted text
> By the way, I am using DBI 1.615 and SQL::Statement 1.31.
Thanks, Jens Show quoted text
> Best regards, > Zhang Jiaqiang > > -----Original Message----- > From: Jens Rehsack via RT [mailto:bug-SQL-Statement@rt.cpan.org] > Sent: 2010年11月17日 18:06 > To: ZHANG Jiaqiang A > Cc: Dan@DWright.Org > Subject: [rt.cpan.org #63072] error when using same table with > different aliases in SQL query > > <URL: https://rt.cpan.org/Ticket/Display.html?id=63072 > > > Hi, > > I'm missing version information in your report, you reported DBD::CSV > 0.31 only - neither DBI nor SQL::Statement. > > Assuming you're using SQL::Statement 1.31, the failiing line is
> > TBLBFETCH: while ( my $array = $tableBobj->fetch_row($data) )
> > Regardless the version you're using, it's not supported (by design) to > use the same table twice in a query. You can workaround by creating > a temporary table so far: > > $dbh->do( "CREATE TEMP TABLE t_people AS SELECT * FROM people" ); $sth > = $dbh->prepare( "select c.husband,h.age,c.wife,w.age from couple > c, people h, t_people w where c.husband = h.name and c.wife = > w.name" ); > > ... > > Best regards, > Jens
This issue is resolved in SQL::Statement 1.32+