Re: Prettyprinting SQL statements
- From: Mensanator <mensanator@xxxxxxx>
- Date: Sat, 10 May 2008 21:16:15 -0700 (PDT)
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],
.
- References:
- Prettyprinting SQL statements
- From: MooJoo
- Prettyprinting SQL statements
- Prev by Date: Re: observer pattern (notification chain synchronization)
- Next by Date: Re: How to call a file
- Previous by thread: Prettyprinting SQL statements
- Next by thread: Re: Compiling Python 2.5.2 on AIX 5.2
- Index(es):
Relevant Pages
|