"No such column" error for table alias defined inside brackets

834 views
Skip to first unread message

Iosipos Mpekenmpaouer

unread,
May 13, 2013, 2:15:03 PM5/13/13
to xer...@googlegroups.com
Hello,

I have the same problem that is described here
http://stackoverflow.com/questions/8112936/sqlexception-no-such-column

When I am running the query directly in Sqlite, everything works fine.

The exception appears in conn.prepareStatement line.

I am using sqlite-jdbc-3.7.15-SNAPSHOT-2.jar in Ubuntu 12.04 with Java 6 Oracle.

Grace B

unread,
May 13, 2013, 6:56:31 PM5/13/13
to xer...@googlegroups.com
Are you referring to the query below?
select * 
from (  person as p 
        inner join company as c on p.p_id=c.p_id
     ) 
     inner join contact as ct on p.p_id=ct.p_id 
 where p.p_id=?;

Iosipos Mpekenmpaouer

unread,
May 14, 2013, 6:28:00 AM5/14/13
to xer...@googlegroups.com


On Tuesday, May 14, 2013 1:56:31 AM UTC+3, Grace B wrote:
Are you referring to the query below?
select * 
from (  person as p 
        inner join company as c on p.p_id=c.p_id
     ) 
     inner join contact as ct on p.p_id=ct.p_id 
 where p.p_id=?;
My query is similar. If I replace the question mark with a value it runs normally in Sqlite, but within Java it throws exception even if I execute it as normal statement (with the value instead of the question mark of course). The error is exactly that :
no such column: p.p_id

Grace B

unread,
May 14, 2013, 7:32:56 AM5/14/13
to xer...@googlegroups.com
Not sure what is going on your end. The following works on my computer. Can you try it on your computer


        Class.forName("org.sqlite.JDBC");

        Connection conn = DriverManager.getConnection("jdbc:sqlite:");

        Statement stat = conn.createStatement();

        stat.execute("create table person (p_id primary key, p_name)");
        stat.execute("insert into person values (1, 'employee')");
        stat.execute("create table contact (p_id primary key, p_name)");
        stat.execute("insert into contact values (1, 'contact')");
        stat.execute("create table company (p_id primary key, p_name)");
        stat.execute("insert into company values (1, 'company')");

        PreparedStatement stmt = conn.prepareStatement(
            "select * from ( person as p " + 
                "inner join company as c on p.p_id=c.p_id " +
                ") " +
                "inner join contact as ct on p.p_id=ct.p_id " +
            "where p.p_id=?;");

        stmt.setInt(1, 1);

        ResultSet rs = stmt.executeQuery();

        rs.next();
        System.out.println(
                rs.getString(1) + "|" + rs.getString(2) + "|" +
                rs.getString(3) + "|" + rs.getString(4) + "|" +
                rs.getString(5) + "|" + rs.getString(6));
Test.java

Iosipos Mpekenmpaouer

unread,
May 14, 2013, 7:49:16 AM5/14/13
to xer...@googlegroups.com
Yes, you are right, that works, but this doesn't:
select * from contact as ct inner join ( person as p inner join company as c on p.p_id=c.p_id )  on p.p_id=ct.p_id where p.p_id=1;

Grace B

unread,
May 14, 2013, 10:43:21 PM5/14/13
to xer...@googlegroups.com
What are you trying to accomplish with these queries?

Iosipos Mpekenmpaouer

unread,
May 15, 2013, 5:59:32 AM5/15/13
to xer...@googlegroups.com
I have some auto-generated queries from Sesame RDF framework. Do you think this is a driver issue?

Grace B

unread,
May 15, 2013, 8:06:15 AM5/15/13
to xer...@googlegroups.com
Nope, it is not a driver issue. 
For SQLite, this the framework is generated a syntactically wrong SQL statement according to http://www.sqlite.org/syntaxdiagrams.html#join-source.
And I actually doubt another that particular query works on any other database.

Anyways, the fix for it is to add a nested select statement as shown below

select * from 
contact as ct 
inner join 
(select * from person as p inner join company as c on p.p_id=c.p_id) as p
on p.p_id=ct.p_id where p.p_id=1

Also note, that this query and the one mentioned at the beginning of this thread will return the same set of data.

Iosipos Mpekenmpaouer

unread,
May 15, 2013, 8:35:20 AM5/15/13
to xer...@googlegroups.com
Well, that's strange, because the query runs normally in Sqlite terminal. Furthermore, according to the syntax diagrams I can't see why this query is wrong. Anyway, I 'll try to figure something out. Thanks for your responses.

Peter Borissow

unread,
May 15, 2013, 9:47:31 AM5/15/13
to xer...@googlegroups.com
Interesting. If the JDBC driver is passing the raw SQL to SQLite and SQLite is throwing an error, I wonder if the SQLite terminal is updating the SQL in some way? Someone would need to throw it into a debugger to see.




From: Iosipos Mpekenmpaouer <iosipos.mp...@gmail.com>
To: xer...@googlegroups.com
Sent: Wednesday, May 15, 2013 8:35 AM
Subject: [xerial 720] Re: "No such column" error for table alias defined inside brackets

Iosipos Mpekenmpaouer

unread,
May 15, 2013, 10:18:08 AM5/15/13
to xer...@googlegroups.com, Peter Borissow
It seems that I have found the problem, and indeed it's not a driver issue. The query runs normally on terminal with Sqlite 3.7.16.2, while with Sqlite 3.7.9 it returns the aforementioned error. Thanks both for your interest and answers.

Grace Batumbya

unread,
May 15, 2013, 1:18:08 PM5/15/13
to xer...@googlegroups.com
On Wed, May 15, 2013 at 10:18 AM, Iosipos Mpekenmpaouer <iosipos.mp...@gmail.com> wrote:
It seems that I have found the problem, and indeed it's not a driver issue. The query runs normally on terminal with Sqlite 3.7.16.2, while with Sqlite 3.7.9 it returns the aforementioned error. Thanks both for your interest and answers.

In that case I will update the driver to use the latest SQLite library. 
Reply all
Reply to author
Forward
0 new messages