wongobongo wrote:
> You mean something like this?
>
> K
>
> ---
>
> # Running a SQL Statement using SQLAlchemy
>
> from sqlalchemy import *
>
> dsn = 'mysql://root:[EMAIL PROTECTED]:3306/tester'
>
> # Create a database engine
> db = create_engine(dsn)
>
> # Do some SQL
> text("INSERT INTO insertable SELECT NULL,txt FROM test_table;",
>      engine=db).execute()
>   
Or directly off the engine itself, including bind parameters.

curs = db.text("select * from my_table where col1 = :col1_value and col2 
= :col2_value order by col1 limit 10 offset 
20").execute(col1_value="hello", col2_value="world")
rs = curs.fetchall()

Even better is to use the sqlalchemy sql constructs. It may seem more 
long winded for short queries but complex queries are much easier to 
express. Especially once the generative selects are introduced :-)

from sqlalchemy import select, and_
my_table = Table('my_table', metadata, ....)

col = my_table.c
criteria = and_(col.col1 == "hello", col.col2 == "world")
s = select([my_table], criteria, orderby=[col.col1], limit=10, offset=20)
curs = s.execute()
rs = curs.fetchal()

This will give you lists of ResultProxy (where each element can have 
it's columns accessed as element[0], element['col1'], or element.col1)

or you can create a query object and get it to return instances of your 
model object (once you map them of course).

eg using select above

query = session.query(MyClass)
rs = query.instances(s.execute())

Huy
> On May 27, 1:18 pm, SamDonaldson <[EMAIL PROTECTED]> wrote:
>   
>> I'd like to be able to run a custom query without having to use any of
>> the ORM stuff.  I'd like to pass in a sql query string to some API
>> that returns back to me instances (it'd be great), or tuples.  How do
>> I do this?
>>
>> Thanks.
>>     
>
>
> >   


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to