[Tutor] Re: insert in postgresql

Lee Harr missive at hotmail.com
Wed Oct 1 17:50:28 EDT 2003


>>>a='cat'
>>>b='dog'
>>>db.query("insert into curso values
(default,"%(a)","%(b)")")^M
SyntaxError: invalid syntax



In python, the single(') and double(") quotes are interchangeable,
and either one can be used to make a triple(""") quote. This is
useful, because a single quote string can contain double quotes
and vice versa. A triple quoted string can contain either one.

I would do it like this:

a = 'cat'
b = 'dog'
q = """insert into cursor values (default '%(a)s', '%(b)s')""" % vars()

db.query(q)


One nice thing about triple quotes is that they can contain newlines:

q = """

INSERT INTO
    cursor
    (foo,
    bar,
    baz)

VALUES
    (default,
    '%(a)s',
    '%(b)s')

""" % vars()


Note that it is always a good idea to include the fields you are inserting
into just in case your db structure changes in the future.

_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8. 
http://join.msn.com/?page=features/junkmail




More information about the Tutor mailing list