insert a dictionary into sql data base

David Bear david.bear at asu.edu
Mon Dec 5 17:20:29 EST 2005


Fredrik Lundh wrote:

> cursor.execute(
>     "INSERT INTO table (%s) VALUES (%%s);" % (",".join(fields)),
>     *values
>     )

Thanks for the hint. However, I don't understand the syntax.

I will be inserting in to postgresql 8.x. I assumed the entire string would
be evaluated prior to being sent to the cursor. However, when I attempt to
manual construct the sql insert statment above I get an error:

>>> print "INSERT INTO table (%s) VALUES (%%s);" % (",".join(fields),
*values)
  File "<stdin>", line 1
    print "INSERT INTO table (%s) VALUES (%%s);" % (",".join(fields),
*values)
                                                                      ^
SyntaxError: invalid syntax


-- 
David Bear
-- let me buy your intellectual property, I want to own your thoughts --



More information about the Python-list mailing list