在python 2.6.4中使用sqlite3标准库,以下查询在sqlite3命令行上正常工作:
select segmentid, node_t, start, number,title from
((segments inner join position using (segmentid))
left outer join titles using (legid, segmentid))
left outer join numbers using (start, legid, version);
但是如果我在python中通过sqlite3库执行它,我会收到错误:
>>> conn=sqlite3.connect('data/test.db')
>>> conn.execute('''select segmentid, node_t, start, number,title from
((segments inner join position using (segmentid)) left outer join titles using
(legid, segmentid)) left outer join numbers using (start, legid, version)''')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: cannot join using column start - column not present
in both tables
连接左侧的(计算)表似乎有相关列,因为如果我自己检查它,我得到:
>>> conn.execute('''select * from ((segments inner join position using
(segmentid)) left outer join titles using
(legid, segmentid)) limit 20''').description
(('segmentid', None, None, None, None, None, None), ('html', None, None, None,
None, None, None), ('node_t', None, None, None, None, None, None), ('legid',
None, None, None, None, None, None), ('version', None, None, None, None, None,
None), ('start', None, None, None, None, None, None), ('title', None, None,
None, None, None, None))
我的架构是:
CREATE TABLE leg (legid integer primary key, t char(16), year char(16),
no char(16));
CREATE TABLE numbers (
number char(16), legid integer, version integer, start integer,
end integer, prev integer, prev_number char(16), next integer,
next_number char(16), primary key (number, legid, version));
CREATE TABLE position (
segmentid integer, legid integer, version integer, start integer,
primary key (segmentid, legid, version));
CREATE TABLE 'segments'
(segmentid integer primary key, html text, node_t integer);
CREATE TABLE titles (legid integer, segmentid integer, title text,
primary key (legid, segmentid));
CREATE TABLE versions
(legid integer, version integer, primary key (legid, version));
CREATE INDEX idx_numbers_start on numbers (legid, version, start);
我对自己做错了什么感到困惑。我试过退出/重启python和sqlite命令行,但看不出我做错了什么。这可能是完全明显的。
答案 0 :(得分:1)
解决方案(使用python库的问题)似乎是引入一个完全虚假的表名:
SELECT legid, version, segmentid, html, node_t, start, number, title
from ((segments inner join position using (segmentid))
left outer join titles using (legid, segmentid)) as LT
left outer join numbers using (start, legid, version);
我认为这样做是强制sqlite收集最外层连接左侧的名称,其中一个是“开始”,然后为最外层的外连接提供一些操作。这对我有用 - 升级可能会引入更多问题而不是删除它们,但是当它到达时我会越过那座桥。
答案 1 :(得分:0)
你有一个有趣名字的表:
CREATE TABLE 'segments'
但我认为这不是问题所在。根据请求,这是我执行的查询,'segments'
表重新创建为segments
:
$ sqlite3 junk.sqlite
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE leg (legid integer primary key, t char(16), year char(16),
no char(16));
CREATE TABLE numbers (
number char(16), legid integer, version integer, start integer,
end integer, prev integer, prev_number char(16), next integer,
next_number char(16), primary key (number, legid, version));
CREATE TABLE position (
segmentid integer, legid integer, version integer, start integer,
primary key (segmentid, legid, version));
CREATE TABLE segments (segmentid integer primary key, html text, node_t integer);
CREATE TABLE titles (legid integer, segmentid integer, title text,
primary key (legid, segmentid));
CREATE TABLE versions
(legid integer, version integer, primary key (legid, version));
CREATE INDEX idx_numbers_start on numbers (legid, version, start);
sqlite> select segmentid, node_t, start, number,title from
...> ((segments inner join position using (segmentid))
...> left outer join titles using (legid, segmentid))
...> left outer join numbers using (start, legid, version);
Error: ambiguous column name: segmentid
答案 2 :(得分:0)
SQLite版本3.6.22 - 看起来您需要限定“模糊列名称”......
sqlite> select segmentid, node_t, start, number,title from
...> ((segments inner join position using (segmentid))
...> left outer join titles using (legid, segmentid))
...> left outer join numbers using (start, legid, version);
Error: ambiguous column name: segmentid
sqlite> select segments.segmentid, node_t, start, number,title from
...> ((segments inner join position using (segmentid))
...> left outer join titles using (legid, segmentid))
...> left outer join numbers using (start, legid, version);
Error: ambiguous column name: start
sqlite> select segments.segmentid, node_t, numbers.start, number,title from
...> ((segments inner join position using (segmentid))
...> left outer join titles using (legid, segmentid))
...> left outer join numbers using (start, legid, version);
sqlite>
行为与SQLite版本3.6.23.1相同