python
  • sql
  • parsing
  • sql-parser
  • 2013-01-24 1 views 9 likes 
    9

    Python 용 SQL 구문 분석 또는 분해 라이브러리가 필요합니다. 우리는 SQL 텍스트 쿼리를 입력 한 다음 그 결과로 쿼리 부분을 다시 가져올 수 있기를 원합니다. 공상이나 그럴 필요는 없지만 과 같은 구문 분석을하지 않는 것이 좋습니다. 도Python 용 SQL 구문 분석 라이브러리

    the_query = "select something from some_table where blah = 'thing' limit 15" 
    query_parts = the_library.parse(the_query) 
    print query_parts.limit().val() 
    
    >>> '15' 
    

    그리고이 : 이상적으로, 우리는 같은 일을 할 수

    the_query = "select something from some_table where blah = 'thing'" 
    query_parts = the_library.parse(the_query) 
    print query_parts.limit().val() 
    
    >>> None 
    

    사람은 우리에게이에 대한 포인터를 줄 수 있습니까? 기능이 더 제한적인 경우에도 괜찮습니다.

    고맙습니다.

    +2

    http://stackoverflow.com/questions/1394998/parsing-sql-with-python 및 HTTP : //navarra.ca/? p = 538 –

    +2

    사실, 필자는 pyparsing 사용을 제안하려고했으나 위의 링크 된 질문은 이미 그렇게합니다. –

    답변

    6

    당신은 노골적 홈페이지에서 도난 sqlparse

    을 살펴하는 것 같아서 :

    >>> # Parsing 
    >>> res = sqlparse.parse('select * from "someschema"."mytable" where id = 1') 
    >>> res 
    <<< (<Statement 'select...' at 0x9ad08ec>,) 
    >>> stmt = res[0] 
    >>> stmt.to_unicode() # converting it back to unicode 
    <<< u'select * from "someschema"."mytable" where id = 1' 
    >>> # This is how the internal representation looks like: 
    >>> stmt.tokens 
    <<< 
    (<DML 'select' at 0x9b63c34>, 
    <Whitespace ' ' at 0x9b63e8c>, 
    <Operator '*' at 0x9b63e64>, 
    <Whitespace ' ' at 0x9b63c5c>, 
    <Keyword 'from' at 0x9b63c84>, 
    <Whitespace ' ' at 0x9b63cd4>, 
    <Identifier '"somes...' at 0x9b5c62c>, 
    <Whitespace ' ' at 0x9b63f04>, 
    <Where 'where ...' at 0x9b5caac>) 
    >>> 
    
    +1

    정확하게 필요한 것. 감사! –

     관련 문제

    • 관련 문제 없음^_^