* Dave Strickler <Dst...@em...> [2002-06-12 16:26 -0400]:
> I'm new to Python and pyPgSQL, and learn best by example. Is there a set
> of example code I could check out?
The source distribution has an examples/ subdirectory with demo code. If you
see a feature you want to use that is not covered by the demos, feel free to
ask here, and I (or somebody else) can give example code for that.
The set of features may be a little overwhelming. From all the features
available, you'll mostly need only these patterns:
# Open a connection to the database
conn = PgSQL.connect(<connection parameters here>)
# Obtain a 'cursor'. This is normally the only object that you'll use.
cursor = conn.cursor()
# Execute a simple SQL statement against the database
cursor.execute("insert into test(name) values ('foo')")
# Execute a SQL statement with appropriate parameter substitution and quoting
# of these parameters done by PyPgSQL.
cursor.execute("select from test where name=%s", ("foo",))
# Iterate over all results and do something with them (here. print the 'id'
# column with all three ways of column access that pyPgSQL supports)
for row in cursor.fetchall():
print row.id, row[0], row["id"]
# Close the cursor.
cursor.close()
# Close the connection.
conn.close()
HTH,
Gerhard
--
mail: gerhard <at> bigfoot <dot> de registered Linux user #64239
web: http://www.cs.fhm.edu/~ifw00065/ OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20 A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))
|