python equivalent of php implode

Jeff Epler jepler at unpythonic.net
Tue Apr 26 11:58:22 EDT 2005


It looks like php's implode(sep, seq) is like sep.join(seq) in Python.

But this is a lousy way to write database queries.  You should use the
Python's DB-API interface's execute(statement, parameters) instead.
Assuming that paramstyle is 'qmark', I think it ends up looking
something like this:

    items = query_param.items()
    keys = [item[0] for item in items]
    values = [item[1] for item in items]

    # If the query parameters or the table are under
    # user control you must take care to validate them
    assert table in permitted_tables
    for k in query_param.keys():
        assert k in permitted_keys

    sql = "INSERT INTO %s (%s) values %s" % (
        table, ", ".join(keys),
        ", ".join(["?"] * len(keys))
    )
    conn.execute(sql, values)

now you don't have to worry that you get the quoting of the values
absolutely right, since db-api does it for you.

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050426/57f9106c/attachment.sig>


More information about the Python-list mailing list