Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import psycopg2
  2.  
  3.  
  4. connection = psycopg2.connect("host=pgteach")
  5. curs = connection.cursor()
  6.  
  7. c = raw_input('insert c for descendants(c,t) -> ')
  8.  
  9. t = raw_input('insert t for descendants(c,t) .> ')
  10.  
  11. # I am selecting the descendents from 2B and excluding the ones that are more than 1 level deeper than node c
  12. # 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.
  13.  
  14. 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))
  15. print curs.fetchall()
  16.  
  17. curs.close()
  18. connection.commit()
  19. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement