| 1 |
import sqlite3.dbapi2 as sqlite |
|---|
| 2 |
|
|---|
| 3 |
con = sqlite.connect(":memory:") |
|---|
| 4 |
crsr = con.cursor() |
|---|
| 5 |
crsr.executescript(open("dumpMenu.sql").read()) |
|---|
| 6 |
|
|---|
| 7 |
crsr.execute('select Name ' |
|---|
| 8 |
'from menu ' |
|---|
| 9 |
'inner join path ' |
|---|
| 10 |
'on path.Id = menu.MenuId ' |
|---|
| 11 |
'group by path.Id ' |
|---|
| 12 |
'having count(*) = 1') # get the children of root |
|---|
| 13 |
print [d[0] for d in crsr.description] |
|---|
| 14 |
|
|---|
| 15 |
#<Output> ({'Name': u'Hijo'}, {'Name': u'OtroHijo'}) </Output> |
|---|
| 16 |
#<Comment> This is ok </Comment> |
|---|
| 17 |
|
|---|
| 18 |
crsr.execute('select menu.Name ' |
|---|
| 19 |
'from menu ' |
|---|
| 20 |
'inner join path ' |
|---|
| 21 |
'on path.Id = menu.MenuId ' |
|---|
| 22 |
'group by path.Id ' |
|---|
| 23 |
'having count(*) = 1') # get the children of root |
|---|
| 24 |
print [d[0] for d in crsr.description] |
|---|
| 25 |
|
|---|
| 26 |
#<Output> ({'menu.Name': u'Hijo'}, {'menu.Name': u'OtroHijo'}) </Output> |
|---|
| 27 |
#<Comment> This is ok (notice now it is "menu.Name" |
|---|
| 28 |
# instead of "Name") </Comment> |
|---|
| 29 |
|
|---|
| 30 |
crsr.execute('select distinct menu.Name ' |
|---|
| 31 |
'from menu ' |
|---|
| 32 |
'inner join path ' |
|---|
| 33 |
'on path.Id = menu.MenuId ' |
|---|
| 34 |
'group by path.Id ' |
|---|
| 35 |
'having count(*) = 1') # get the children of root |
|---|
| 36 |
print [d[0] for d in crsr.description] |
|---|
| 37 |
|
|---|
| 38 |
#<Output> ({'menu.Name': u'Hijo'}, {'menu.Name': u'OtroHijo'}) </Output> |
|---|
| 39 |
#<Comment> This is ok (with "distinct" I still get the |
|---|
| 40 |
# same result "menu.Name") (1) </Comment> |
|---|
| 41 |
|
|---|
| 42 |
crsr.execute("select distinct menu.Name " |
|---|
| 43 |
"from menu " |
|---|
| 44 |
"inner join path " |
|---|
| 45 |
"on path.Id = menu.MenuId " |
|---|
| 46 |
"left join (select MenuId as 'pId' " |
|---|
| 47 |
"from menu " |
|---|
| 48 |
"where name = 'Hijo') Parent " |
|---|
| 49 |
"where Id in (select Id " |
|---|
| 50 |
"from path " |
|---|
| 51 |
"where AncestorId = Parent.pId) " |
|---|
| 52 |
"and Id not in " |
|---|
| 53 |
"(select Id " |
|---|
| 54 |
"from path " |
|---|
| 55 |
"where Id in(select Id " |
|---|
| 56 |
"from path " |
|---|
| 57 |
"where AncestorId = Parent.pId) " |
|---|
| 58 |
"and AncestorId not in " |
|---|
| 59 |
"(select AncestorId " |
|---|
| 60 |
"from path " |
|---|
| 61 |
"where Id = Parent.pId) " |
|---|
| 62 |
"and AncestorId != Parent.pId) ") |
|---|
| 63 |
print [d[0] for d in crsr.description] |
|---|
| 64 |
|
|---|
| 65 |
#<Output> ({'Name': u'Hijo-1'}, {'Name': u'Hijo-2'}) </Output> |
|---|
| 66 |
#<Comment> Ooops!!! Here I'm using "select distinct menu.name" in the |
|---|
| 67 |
# main select |
|---|
| 68 |
# as in the previous query (1) but I'm getting "Name" as key |
|---|
| 69 |
# instead of "menu.Name". |
|---|
| 70 |
# This is the inconsistency I was talking about. |
|---|
| 71 |
#</Comment> |
|---|