you should be able to use conn.execute(anystring, [list of dict]) just  
fine.   the only thing that wont work at the moment is autocommit so  
youd have to run a begin()/commit() pair.

to bring the string version of REPLACE into SQLAlchemy bind parameter  
processing etc. use text() - the basic idea (havent tested this code,  
may need tweaks) is this:

def replace(table, keys):
        statement = 'REPLACE INTO %s(%s) VALUES (%s)' %
        (
                table.name,
                ','.join(table.c[key].name for key in keys),
                ','.join(":" + table.c[key] for key in keys))
        )
        return text(statement, bindparams=[bindparam(key,  
type_=table.c[key].type) for key in keys])

trans = conn.begin()
conn.execute(replace(mytable, ['one', 'two', 'three']), [{'one':1,  
'two':2, 'three':3}, {...}, {...}, ...])
trans.commit()

On Jan 4, 2008, at 1:27 PM, Mike Bernson wrote:

>
> I have code that need to do a mysql replace instead of an insert.
>
> The quick hack to just create a string and execute has problems with
> quoting things, sql injection ...
>
> If a table.replace() is not easy to do is there a way to create a  
> statement
> that works that way. If I could do a conn.execute(statement, [list of
> dict]) that
> would be great.
>
> The current code look like:
> table = mappings.TableForTag(setName)
>
>    keys = sorted([str(k) for k in records[0].keys() if
> table.c.has_key((str(k)))])
>    statement = 'REPLACE INTO %s(%s) VALUES ' %(table.name,
>        ','.join(["`%s`" % k for k in keys]))
>    values = []
>    for record in records:
>        values.append('(%s)' % ','.join(["'%s'" % str(record.get(k))
>            for k in keys]))
>    values = ','.join(values)
>    conn = engine.connect()
>    conn.execute(statement + values)
>
> I would like to be able to do something like:
>
> table = mappings.TableForTag(setName)
> conn = engine.connect()
> conn.execute(table.replace(), records)
>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
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