Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / psycopg2 / examples / simple.py
1 # simple.py - very simple example of plain DBAPI-2.0 usage
2 # currently used as test-me-stress-me script for psycopg 2.0
3 #
4 # Copyright (C) 2001-2003 Federico Di Gregorio  <fog@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2, or (at your option) any later
9 # version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
13 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 # for more details.
15
16 ## put in DSN your DSN string
17
18 DSN = 'dbname=test'
19
20 ## don't modify anything below this line (except for experimenting)
21
22 class SimpleQuoter(object):
23     def sqlquote(x=None):
24         return "'bar'"
25
26 import sys
27 import psycopg2
28
29 if len(sys.argv) > 1:
30     DSN = sys.argv[1]
31
32 print "Opening connection using dns:", DSN
33 conn = psycopg2.connect(DSN)
34 print "Encoding for this connection is", conn.encoding
35
36 curs = conn.cursor()
37 curs.execute("SELECT 1 AS foo")
38 print curs.fetchone()
39 curs.execute("SELECT 1 AS foo")
40 print curs.fetchmany()
41 curs.execute("SELECT 1 AS foo")
42 print curs.fetchall()
43
44 conn.rollback()
45
46 sys.exit(0)
47
48 curs.execute("SELECT 1 AS foo", async=1)
49
50 curs.execute("SELECT %(foo)s AS foo", {'foo':'bar'})
51 curs.execute("SELECT %(foo)s AS foo", {'foo':None})
52 curs.execute("SELECT %(foo)f AS foo", {'foo':42})
53 curs.execute("SELECT %(foo)s AS foo", {'foo':SimpleQuoter()})