Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- connection = psycopg2.connect("host=pgteach")
- curs = connection.cursor()
- c = raw_input('insert c for descendants(c,t) -> ')
- t = raw_input('insert t for descendants(c,t) .> ')
- # I am selecting the descendents from 2B and excluding the ones that are more than 1 level deeper than node c
- # For the excluding part I am joining a table with all the elements under c, and looking if the element with tag t is nested within another element which makes it more than 1 level under c and thus needs to be excluded.
- curs.execute("SELECT * FROM Elements WHERE tag=%s and start > %s and stop < (SELECT stop FROM Elements WHERE start=%s) EXCEPT SELECT b.start, b.stop, b.tag FROM (SELECT * FROM Elements WHERE start>%s) a, (SELECT * FROM Elements WHERE tag=%s and start > %s and stop < (SELECT stop FROM Elements WHERE start=%s)) b WHERE a.start<b.start and b.stop<a.stop;",(t,c,c,c,t,c,c))
- print curs.fetchall()
- curs.close()
- connection.commit()
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement