librelist archives

« back to archive

flaskext.database proposal

flaskext.database proposal

From:
Dan Jacob
Date:
2010-05-07 @ 13:59
I've added a snippet at http://flask.pocoo.org/snippets/11/ which
shows how to use Tornado's MySQLdb wrapper with Flask.

This seems to be a useful lightweight, useful extension to MySQLdb.
While a powerful toolkit, SQLAlchemy is a complex beast for beginners
to master and the overhead may not be warranted in a small project. In
addition, many experienced developers prefer not to deal with an ORM
and are quite happy working in SQL.

Flask quite rightfully is database-agnostic. However I was thinking
that a lightweight API around some common DB API implementations would
be useful, as the basic DB API is not the most friendly and Pythonic
of interfaces. It might not be necessary to have a completely
database-agnostic wrapper - for that you are better off with
SQLAlchemy - but something that makes working with raw SQL a little
less painful.

(Another example  is the basic DB API provided in web.py:
http://webpy.org/tutorial3.en#databasing. )

What I propose is a flaskext.database extension library, with
subpackages for mysql, postgres and sqlite (with additional database
support as and when). Another name might be "flaskext.sql" as opposed
to no-SQL databases such as MongoDB.

Would this be useful to anyone ?

Re: [flask] flaskext.database proposal

From:
Oliver Rutherfurd
Date:
2010-05-07 @ 14:38
On Fri, May 7, 2010 at 9:59 AM, Dan Jacob <danjac354@gmail.com> wrote:
> I've added a snippet at http://flask.pocoo.org/snippets/11/ which
> shows how to use Tornado's MySQLdb wrapper with Flask.
>
> This seems to be a useful lightweight, useful extension to MySQLdb.
> While a powerful toolkit, SQLAlchemy is a complex beast for beginners
> to master and the overhead may not be warranted in a small project. In
> addition, many experienced developers prefer not to deal with an ORM
> and are quite happy working in SQL.

One doesn't need to use SA's ORM.  Here's the equivalent of your
example, with the bonus that it already supports all those other
databases ;-).

from sqlalchemy import create_engine

g.db = create_engine('sqlite://')

newsitems = g.db.execute("select * from newsitems")

-Ollie

-- 
http://rutherfurd.net/

Re: [flask] flaskext.database proposal

From:
Dan Jacob
Date:
2010-05-07 @ 14:59
Sure - SQLAlchemy has a nice lower level library. The one thing it
doesn't do is support a simple object syntax - you have to do e.g.
newsitem[0] or newsitem['title'] but you can't do newsitem.title
(unless you go to the trouble of using a mapper or declarative class -
but then you are using an ORM).

Generally though if you are writing raw SQL then a common API is less
important as the SQL itself is likely to contain DB-specific syntax.


On 7 May 2010 15:38, Oliver Rutherfurd <oliver@rutherfurd.net> wrote:
> On Fri, May 7, 2010 at 9:59 AM, Dan Jacob <danjac354@gmail.com> wrote:
>> I've added a snippet at http://flask.pocoo.org/snippets/11/ which
>> shows how to use Tornado's MySQLdb wrapper with Flask.
>>
>> This seems to be a useful lightweight, useful extension to MySQLdb.
>> While a powerful toolkit, SQLAlchemy is a complex beast for beginners
>> to master and the overhead may not be warranted in a small project. In
>> addition, many experienced developers prefer not to deal with an ORM
>> and are quite happy working in SQL.
>
> One doesn't need to use SA's ORM.  Here's the equivalent of your
> example, with the bonus that it already supports all those other
> databases ;-).
>
> from sqlalchemy import create_engine
>
> g.db = create_engine('sqlite://')
>
> newsitems = g.db.execute("select * from newsitems")
>
> -Ollie
>
> --
> http://rutherfurd.net/
>

Re: [flask] flaskext.database proposal

From:
Oliver Rutherfurd
Date:
2010-05-07 @ 16:29
On Fri, May 7, 2010 at 10:59 AM, Dan Jacob <danjac354@gmail.com> wrote:
> Sure - SQLAlchemy has a nice lower level library. The one thing it
> doesn't do is support a simple object syntax - you have to do e.g.
> newsitem[0] or newsitem['title'] but you can't do newsitem.title
> (unless you go to the trouble of using a mapper or declarative class -
> but then you are using an ORM).

I think newsitem.title works as of 0.6, where namedtuples are used.

-Ollie

Re: [flask] flaskext.database proposal

From:
Dan Jacob
Date:
2010-05-07 @ 16:31
Ollie: if that is the case (I might have missed that feature) then
that's about 99.9% of what I need ;-)

On 7 May 2010 17:29, Oliver Rutherfurd <oliver@rutherfurd.net> wrote:
> On Fri, May 7, 2010 at 10:59 AM, Dan Jacob <danjac354@gmail.com> wrote:
>> Sure - SQLAlchemy has a nice lower level library. The one thing it
>> doesn't do is support a simple object syntax - you have to do e.g.
>> newsitem[0] or newsitem['title'] but you can't do newsitem.title
>> (unless you go to the trouble of using a mapper or declarative class -
>> but then you are using an ORM).
>
> I think newsitem.title works as of 0.6, where namedtuples are used.
>
> -Ollie
>

Re: [flask] flaskext.database proposal

From:
Thadeus Burgess
Date:
2010-05-07 @ 15:16
What you really want is something like web2py's DAL then.

You can use it with a flask application (I have).

--
Thadeus





On Fri, May 7, 2010 at 9:59 AM, Dan Jacob <danjac354@gmail.com> wrote:
> Sure - SQLAlchemy has a nice lower level library. The one thing it
> doesn't do is support a simple object syntax - you have to do e.g.
> newsitem[0] or newsitem['title'] but you can't do newsitem.title
> (unless you go to the trouble of using a mapper or declarative class -
> but then you are using an ORM).
>
> Generally though if you are writing raw SQL then a common API is less
> important as the SQL itself is likely to contain DB-specific syntax.
>
>
> On 7 May 2010 15:38, Oliver Rutherfurd <oliver@rutherfurd.net> wrote:
>> On Fri, May 7, 2010 at 9:59 AM, Dan Jacob <danjac354@gmail.com> wrote:
>>> I've added a snippet at http://flask.pocoo.org/snippets/11/ which
>>> shows how to use Tornado's MySQLdb wrapper with Flask.
>>>
>>> This seems to be a useful lightweight, useful extension to MySQLdb.
>>> While a powerful toolkit, SQLAlchemy is a complex beast for beginners
>>> to master and the overhead may not be warranted in a small project. In
>>> addition, many experienced developers prefer not to deal with an ORM
>>> and are quite happy working in SQL.
>>
>> One doesn't need to use SA's ORM.  Here's the equivalent of your
>> example, with the bonus that it already supports all those other
>> databases ;-).
>>
>> from sqlalchemy import create_engine
>>
>> g.db = create_engine('sqlite://')
>>
>> newsitems = g.db.execute("select * from newsitems")
>>
>> -Ollie
>>
>> --
>> http://rutherfurd.net/
>>
>

Re: [flask] flaskext.database proposal

From:
Armin Ronacher
Date:
2010-05-07 @ 15:23
Hi,

On 2010-05-07 5:16 PM, Thadeus Burgess wrote:
> What you really want is something like web2py's DAL then.
I think SQLAlchemy is the better choice there.


Regards,
Armin

Re: [flask] flaskext.database proposal

From:
Thadeus Burgess
Date:
2010-05-07 @ 15:34
I mention it because it already does what you want.

rows = db.executesql("select * from newsitems")
for row in rows:
   print row.title

The web2py DAL is low level, and for me, using the DAL feels closer to
SQL than anything else out there.

--
Thadeus





On Fri, May 7, 2010 at 10:23 AM, Armin Ronacher
<armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 2010-05-07 5:16 PM, Thadeus Burgess wrote:
>> What you really want is something like web2py's DAL then.
> I think SQLAlchemy is the better choice there.
>
>
> Regards,
> Armin
>

Re: [flask] flaskext.database proposal

From:
Sebastian Benthall
Date:
2010-05-07 @ 16:04
Just wanted to say that I'd be excited to see this sort of feature in Flask.

On Fri, May 7, 2010 at 11:34 AM, Thadeus Burgess <thadeusb@thadeusb.com>wrote:

> I mention it because it already does what you want.
>
> rows = db.executesql("select * from newsitems")
> for row in rows:
>   print row.title
>
> The web2py DAL is low level, and for me, using the DAL feels closer to
> SQL than anything else out there.
>
> --
> Thadeus
>
>
>
>
>
> On Fri, May 7, 2010 at 10:23 AM, Armin Ronacher
> <armin.ronacher@active-4.com> wrote:
> > Hi,
> >
> > On 2010-05-07 5:16 PM, Thadeus Burgess wrote:
> >> What you really want is something like web2py's DAL then.
> > I think SQLAlchemy is the better choice there.
> >
> >
> > Regards,
> > Armin
> >
>

Re: [flask] flaskext.database proposal

From:
Dan Jacob
Date:
2010-05-07 @ 16:22
The idea is to just have a lightweight wrapper around DB API - if you
need anything more (ORM, connection pooling, etc) then SQLAlchemy is
the best choice.

On 7 May 2010 17:04, Sebastian Benthall <sbenthall@gmail.com> wrote:
> Just wanted to say that I'd be excited to see this sort of feature in Flask.
>
> On Fri, May 7, 2010 at 11:34 AM, Thadeus Burgess <thadeusb@thadeusb.com>
> wrote:
>>
>> I mention it because it already does what you want.
>>
>> rows = db.executesql("select * from newsitems")
>> for row in rows:
>>   print row.title
>>
>> The web2py DAL is low level, and for me, using the DAL feels closer to
>> SQL than anything else out there.
>>
>> --
>> Thadeus
>>
>>
>>
>>
>>
>> On Fri, May 7, 2010 at 10:23 AM, Armin Ronacher
>> <armin.ronacher@active-4.com> wrote:
>> > Hi,
>> >
>> > On 2010-05-07 5:16 PM, Thadeus Burgess wrote:
>> >> What you really want is something like web2py's DAL then.
>> > I think SQLAlchemy is the better choice there.
>> >
>> >
>> > Regards,
>> > Armin
>> >
>
>