FAQ
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.

Search Discussions

  • Mensanator at May 11, 2008 at 4:16 am

    On May 10, 10:23?pm, MooJoo 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],

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 11, '08 at 3:23a
activeMay 11, '08 at 4:16a
posts2
users2
websitepython.org

2 users in discussion

Mensanator: 1 post MooJoo: 1 post

People

Translate

site design / logo © 2022 Grokbase