Re: Prettyprinting SQL statements



On May 10, 10:23�pm, MooJoo <flossy@xxxxxxxxxxxxxxxxx> wrote:
I'm building a Python app that will be making queries to a MySQL server
using the MySQLdb module. The app will need to create SQL statements on
the fly which, in addition to going to the server, will occasionally
need to be displayed to the user. While these queries are not too
complex, they still can be difficult to decipher without doing some
formatting. I've done the requisite Googling to see if a library to
format SQL can be found but, other than commericial Windows apps and
some on-line formatters, I've not found anything remotely usable. Before
trying to write my own parser/formatter, I was hoping somebody might
know of a package to perform this function.

Anybody? Ferris? Anybody at all?

Thanks.

I always just do this:

def keyword(s):
if s.isupper():
return '\n'+s+'\t'
else:
return s
sql = "SELECT letter.*,letter1.* FROM letter,letter AS letter1 ORDER
BY letter.n;"
s = sql.split()
pp = ' '.join(map(keyword,s))
print pp

Then insert the pp strings into my source code as shown.

# unjoined tables create a Cartesian Product
#
import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.executescript("""
create table letter(n);
""")

letters = [('a'),('b'),('c'),('d'),('e'),('f'),('g'),('h')]

cur.executemany("""
INSERT
INTO letter(n)
VALUES (?);
"""
, letters)

cur.execute("""
SELECT letter.*,letter1.*
FROM letter,letter
AS letter1
ORDER
BY letter.n;
""")

cartesian_product = cur.fetchall()

for i in cartesian_product:
print i[0]+i[1],
.



Relevant Pages

  • Re: A couple of questions
    ... You can format data in a CListBox; it depends on how sophisticated your formatting has to ... Take a look at my Logging Listbox control on my MVP Tips site. ... Edit controls of any flavor tend to be rather miserable to use as logging controls ... I have a dialog-based app with a CRichEditCtrl. ...
    (microsoft.public.vc.mfc)
  • Re: Passing parameters from page to page in HTA app
    ... > At the moment I have a small app that gives them a list of events. ... > add/delete from the list or select an event and select edit. ... > itinerary for that event as well as add client names to the event. ... > involve formatting the collected data and outputting it as a .doc file. ...
    (microsoft.public.scripting.jscript)
  • Re: FORMAT_STRING and Regional Settings
    ... this formatting is controlled by the LocaleIdentifier connection string ... Dim clConnection As New AdomdConnection ... When the same code is run from the windows app (replacing Response.Write ... when you change the regional formatting settings to French ...
    (microsoft.public.sqlserver.olap)
  • Re: VBA super slow to execute in PP2007
    ... Today I separated out the drawing of the shapes and the formatting. ... drawing zips along - no difference to PP2003. ... steve at-sign pptools dot com ... Intel processor, lots of memory, NOD32 is the AV app I'm using and it's set to ...
    (microsoft.public.powerpoint)
  • Re: Tasty exceptions
    ... I maintain a .NET app which suffers from this problem. ... errors in SQL statements present themselves as ... The best way to bug Borland about bugs. ... I wasn't asleep, I was drunk." ...
    (borland.public.delphi.non-technical)