Parsing sql statements like the following is dead simple with PyParsing. INSERT INTO table_a VALUES ('A', 'B', 'C'); INSERT INTO table_a VALUES ('D', 'E', 'F'); This is the program : from pyparsing import Suppress, delimitedList, sglQuotedString, removeQuotes sglQuotedString.setParseAction( removeQuotes ) stmt = Suppress("INSERT INTO table_a VALUES (") + delimitedList(sglQuotedString) \ + Suppress(");") f = open('some_file.sql') lines=f.read() for tokens, start,end in stmt.scanString(lines): print tokens f.close() PyParsing already knows about quoted strings. The sglQuotedString type matches a single quoted string, which is handy for strings in SQL.
Oracle 10g has a feature where it gathers optimizer statistics automatically at night. This is done through a scheduled job called GATHER_STATS_JOB. If you want to disable this, you have to issue : EXEC dbms_scheduler.disable(’GATHER_STATS_JOB’); To enable it again : EXEC dbms_scheduler.enable(’GATHER_STATS_JOB’); The following query shows when this job was run : SELECT * FROM dba_scheduler_job_run_details WHERE job_name = ’GATHER_STATS_JOB’; There are other tables, all starting with dba_scheduler, that show the schedules and so on.
The day after I hyped the Getting Started with PyParsing book, I got to use it. Here’s the script : from pyparsing import SkipTo, Suppress, CaselessLiteral import glob # Example to match # delete from TABLENAME # where CUSTOMER_ID = 'INTERNAL'; table_name = SkipTo("where") where_clause = SkipTo(';') delete_stmt = Suppress("delete") + Suppress("from") + table_name + Suppress("where") + where_clause + Suppress(";") for filename in glob.glob('*.sql'): f = open(filename) print '-- ', filename, ':' lines=f.
To get the language setting of the current session : SELECT USERENV ('language') FROM DUAL; This returns the database character set, not the user’s though. The NLS settings can be found in the following list, in this order. If a value is specified in NLS_SESSION_PARAMETERS, it has preference over a value in NLS_INSTANCE_PARAMETERS. NLS_SESSION_PARAMETERS NLS_INSTANCE_PARAMETERS NLS_DATABASE_PARAMETERS
I just bought and read the Getting Started with Pyparsing PDF book. And it’s good. PyParsing is a way of building a parser using Python code. You should think Yacc/Lex, but readable. It can be used to parse text, and it can also handle HTML. This is the example from the PyParsing website : from pyparsing import Word, alphas greet = Word( alphas ) + "," + Word( alphas ) + "!
Copyright (c) 2025 Michel Hollands