Pythonic wrappers for SQL?

Stian Soiland stian at soiland.no
Mon Jan 16 17:48:01 CET 2006


On 1/14/06, EleSSaR^ <elessar.xyz at despammed.com> wrote:
> Kenneth McDonald si è profuso/a a scrivere su comp.lang.python tutte queste
> elucubrazioni:
>
> > there any good libraries out there that let one write (basic) queries
> > in a Pythonic syntax, rather than directly in SQL?
>
> You need an ORM. Beyond SQLAlchemy (I don't have experience with it) i
> would suggest you try out SQLObject and PyDO.
>
> Just a clue: although they prevent you from writing SQL, you'll have to
> learn SQL basics anyway, or you won't understand the docs.
>
> If you simply want to forget SQL, you can try out Axiom:

Or - as a word play in this case - try my module ForgetSQL [1]

(Also available in a new and backwards-incompatible beta version [2]
which should be easier to get started with, has more extensive unit
tests, but has only been tested with MySQL and SQLite)

[1] http://soiland.no/software/forgetsql/
[2] http://soiland.no/i/src/forgetsql2/

Example code from [2]

    import MySQLdb, forgetsql2
    # Connect to MySQLdb using keyword parameters
    db = forgetsql2.generate(MySQLdb, {db='fish'})

    # Iterate through generated class from the table "postal"
    for postal in db.Postal:
        # Print normal fields
        print postal.postal_no, postal.postal_name, postal.municipal_id
        # Follow the foreign key municipal_id to retrieve the entry
        # from the Municipal class
        municipal = postal.get_municipal()
        print municipal.municipal_name

    # Retrieve by primary key
    rogaland = db.County(county_id=11)
    # Iterate over municipals that have foreign keys to rogaland
    for municipal in rogaland.get_municipals():
        print municipal.municipal_name

    # Load by primary key, change, and save
    postal = db.Postal(postal_id=4042)
    postal.postal_name = "Fish land"
    postal.save()


--
Stian Søiland               In theory there is no difference between theory
Birmingham, UK              and practice. In practice there is. [Berra]
http://soiland.no/
                     =/\=



More information about the Python-list mailing list