~psycopg/psycopg/2.0.x

« back to all changes in this revision

Viewing changes to examples/cursor.py

  • Committer: Federico Di Gregorio
  • Date: 2007-04-10 06:36:18 UTC
  • Revision ID: fog-7050d8d8d2669efe71403b32fda453f9b1f6a470
Fixed both Python 2.5 and 64 bit problems.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
## don't modify anything below this line (except for experimenting)
20
20
 
21
21
import sys
22
 
import psycopg
23
 
import psycopg.extensions
 
22
import psycopg2
 
23
import psycopg2.extensions
24
24
 
25
25
if len(sys.argv) > 1:
26
26
    DSN = sys.argv[1]
27
27
 
28
28
print "Opening connection using dsn:", DSN
29
 
 
30
 
conn = psycopg.connect(DSN)
 
29
conn = psycopg2.connect(DSN)
31
30
print "Encoding for this connection is", conn.encoding
32
31
 
33
32
 
34
 
class NoDataError(psycopg.ProgrammingError):
 
33
class NoDataError(psycopg2.ProgrammingError):
35
34
    """Exception that will be raised by our cursor."""
36
35
    pass
37
36
 
38
 
class Cursor(psycopg.extensions.cursor):
 
37
class Cursor(psycopg2.extensions.cursor):
39
38
    """A custom cursor."""
40
39
 
41
40
    def fetchone(self):
46
45
        uses the same function to fetch rows, the code path from Python is
47
46
        different.
48
47
        """
49
 
        d = psycopg.extensions.cursor.fetchone(self)
 
48
        d = psycopg2.extensions.cursor.fetchone(self)
50
49
        if d is None:
51
50
            raise NoDataError("no more data")
52
51
        return d
53
52
    
54
 
curs = conn.cursor(factory=Cursor)
 
53
curs = conn.cursor(cursor_factory=Cursor)
55
54
curs.execute("SELECT 1 AS foo")
56
55
print "Result of fetchone():", curs.fetchone()
57
56