|
psycopg2
2.4.5
|
00001 # simple.py - very simple example of plain DBAPI-2.0 usage 00002 # 00003 # currently used as test-me-stress-me script for psycopg 2.0 00004 # 00005 # Copyright (C) 2001-2010 Federico Di Gregorio <fog@debian.org> 00006 # 00007 # psycopg2 is free software: you can redistribute it and/or modify it 00008 # under the terms of the GNU Lesser General Public License as published 00009 # by the Free Software Foundation, either version 3 of the License, or 00010 # (at your option) any later version. 00011 # 00012 # psycopg2 is distributed in the hope that it will be useful, but WITHOUT 00013 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00015 # License for more details. 00016 00017 ## put in DSN your DSN string 00018 00019 DSN = 'dbname=test' 00020 00021 ## don't modify anything below this line (except for experimenting) 00022 00023 class SimpleQuoter(object): 00024 def sqlquote(x=None): 00025 return "'bar'" 00026 00027 import sys 00028 import psycopg2 00029 00030 if len(sys.argv) > 1: 00031 DSN = sys.argv[1] 00032 00033 print "Opening connection using dns:", DSN 00034 conn = psycopg2.connect(DSN) 00035 print "Encoding for this connection is", conn.encoding 00036 00037 curs = conn.cursor() 00038 curs.execute("SELECT 1 AS foo") 00039 print curs.fetchone() 00040 curs.execute("SELECT 1 AS foo") 00041 print curs.fetchmany() 00042 curs.execute("SELECT 1 AS foo") 00043 print curs.fetchall() 00044 00045 conn.rollback() 00046 00047 sys.exit(0) 00048 00049 curs.execute("SELECT 1 AS foo", async=1) 00050 00051 curs.execute("SELECT %(foo)s AS foo", {'foo':'bar'}) 00052 curs.execute("SELECT %(foo)s AS foo", {'foo':None}) 00053 curs.execute("SELECT %(foo)f AS foo", {'foo':42}) 00054 curs.execute("SELECT %(foo)s AS foo", {'foo':SimpleQuoter()})