~groldster/ubuntu/maverick/psycopg2/merge-611040

« back to all changes in this revision

Viewing changes to examples/copy_to.py

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2006-08-09 10:28:30 UTC
  • Revision ID: james.westby@ubuntu.com-20060809102830-grac1dsp24uyqfp4
Tags: upstream-2.0.4
ImportĀ upstreamĀ versionĀ 2.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# copy_to.py -- example about copy_to 
 
2
#
 
3
# Copyright (C) 2002 Tom Jenkins <tjenkins@devis.com>
 
4
# Copyright (C) 2005 Federico Di Gregorio <fog@initd.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
 
 
17
## put in DSN your DSN string
 
18
 
 
19
DSN = 'dbname=test'
 
20
 
 
21
## don't modify anything below tis line (except for experimenting)
 
22
 
 
23
import sys
 
24
import os
 
25
import StringIO
 
26
import psycopg2
 
27
 
 
28
if len(sys.argv) > 1:
 
29
    DSN = sys.argv[1]
 
30
 
 
31
print "Opening connection using dns:", DSN
 
32
conn = psycopg2.connect(DSN)
 
33
print "Encoding for this connection is", conn.encoding
 
34
 
 
35
curs = conn.cursor()
 
36
try:
 
37
    curs.execute("CREATE TABLE test_copy (fld1 text, fld2 text, fld3 int4)")
 
38
except:
 
39
    conn.rollback()
 
40
    curs.execute("DROP TABLE test_copy")
 
41
    curs.execute("CREATE TABLE test_copy (fld1 text, fld2 text, fld3 int4)")
 
42
conn.commit()
 
43
 
 
44
# demostrate copy_to functionality
 
45
data = [('Tom', 'Jenkins', '37'),
 
46
        ('Madonna', None, '45'),
 
47
        ('Federico', 'Di Gregorio', None)]
 
48
query = "INSERT INTO test_copy VALUES (%s, %s, %s)"
 
49
curs.executemany(query, data)
 
50
conn.commit()
 
51
 
 
52
# copy_to using defaults
 
53
io = open('copy_to.txt', 'w')
 
54
curs.copy_to(io, 'test_copy')
 
55
print "1) Copy %d records into file object using defaults: " % len (data) + \
 
56
      "sep = \\t and null = \\N"
 
57
io.close()
 
58
 
 
59
rows = open('copy_to.txt', 'r').readlines()
 
60
print "   File has %d rows:" % len(rows)
 
61
 
 
62
for r in rows:
 
63
    print "   ", r,
 
64
 
 
65
# copy_to using custom separator
 
66
io = open('copy_to.txt', 'w')
 
67
curs.copy_to(io, 'test_copy', ':')
 
68
print "2) Copy %d records into file object using sep = :" % len(data)
 
69
io.close()
 
70
 
 
71
rows = open('copy_to.txt', 'r').readlines()
 
72
print "   File has %d rows:" % len(rows)
 
73
 
 
74
for r in rows:
 
75
    print "   ", r,
 
76
 
 
77
# copy_to using custom null identifier
 
78
io = open('copy_to.txt', 'w')
 
79
curs.copy_to(io, 'test_copy', null='NULL')
 
80
print "3) Copy %d records into file object using null = NULL" % len(data)
 
81
io.close()
 
82
 
 
83
rows = open('copy_to.txt', 'r').readlines()
 
84
print "   File has %d rows:" % len(rows)
 
85
 
 
86
for r in rows:
 
87
    print "   ", r,
 
88
 
 
89
# copy_to using custom separator and null identifier
 
90
io = open('copy_to.txt', 'w')
 
91
curs.copy_to(io, 'test_copy', ':', 'NULL')
 
92
print "4) Copy %d records into file object using sep = : and null ) NULL" % \
 
93
      len(data)
 
94
io.close()
 
95
 
 
96
rows = open('copy_to.txt', 'r').readlines()
 
97
print "   File has %d rows:" % len(rows)
 
98
 
 
99
for r in rows:
 
100
    print "   ", r,
 
101
 
 
102
curs.execute("DROP TABLE test_copy")
 
103
os.unlink('copy_to.txt')
 
104
conn.commit()