psycopg2/examples/copy_from.py

178 lines
4.7 KiB
Python
Raw Normal View History

2005-03-01 19:41:02 +03:00
# copy_from.py -- example about copy_from
#
# Copyright (C) 2002 Tom Jenkins <tjenkins@devis.com>
# Copyright (C) 2005 Federico Di Gregorio <fog@initd.org>
#
2014-05-20 20:50:53 +04:00
# psycopg2 is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2005-03-01 19:41:02 +03:00
#
2014-05-20 20:50:53 +04:00
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
2005-03-01 19:41:02 +03:00
## put in DSN your DSN string
DSN = 'dbname=test'
## don't modify anything below this line (except for experimenting)
2005-03-01 19:41:02 +03:00
import sys
import os
import StringIO
import psycopg2
2005-03-01 19:41:02 +03:00
if len(sys.argv) > 1:
DSN = sys.argv[1]
2017-11-28 11:30:15 +03:00
print("Opening connection using dsn:", DSN)
conn = psycopg2.connect(DSN)
2017-11-28 11:30:15 +03:00
print("Encoding for this connection is", conn.encoding)
2005-03-01 19:41:02 +03:00
curs = conn.cursor()
try:
curs.execute("CREATE TABLE test_copy (fld1 text, fld2 text, fld3 int4)")
except:
conn.rollback()
curs.execute("DROP TABLE test_copy")
curs.execute("CREATE TABLE test_copy (fld1 text, fld2 text, fld3 int4)")
conn.commit()
# copy_from with default arguments, from open file
io = open('copy_from.txt', 'wr')
data = ['Tom\tJenkins\t37\n',
2007-04-13 18:07:11 +04:00
'Madonna\t\\N\t45\n',
'Federico\tDi Gregorio\t\\N\n']
2005-03-01 19:41:02 +03:00
io.writelines(data)
io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy')
2017-11-28 11:30:15 +03:00
print("1) Copy %d records from file object " % len(data) +
"using defaults (sep: \\t and null = \\N)")
2005-03-01 19:41:02 +03:00
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
2017-11-28 11:30:15 +03:00
print(" Select returned %d rows" % len(rows))
2005-03-01 19:41:02 +03:00
for r in rows:
2017-11-28 11:30:15 +03:00
print(" %s %s\t%s" % (r[0], r[1], r[2]))
2005-03-01 19:41:02 +03:00
curs.execute("delete from test_copy")
conn.commit()
# copy_from using custom separator, from open file
io = open('copy_from.txt', 'wr')
data = ['Tom:Jenkins:37\n',
'Madonna:\N:45\n',
'Federico:Di Gregorio:\N\n']
io.writelines(data)
io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy', ':')
2017-11-28 11:30:15 +03:00
print("2) Copy %d records from file object using sep = :" % len(data))
2005-03-01 19:41:02 +03:00
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
2017-11-28 11:30:15 +03:00
print(" Select returned %d rows" % len(rows))
2005-03-01 19:41:02 +03:00
for r in rows:
2017-11-28 11:30:15 +03:00
print(" %s %s\t%s" % (r[0], r[1], r[2]))
2005-03-01 19:41:02 +03:00
curs.execute("delete from test_copy")
conn.commit()
# copy_from using custom null identifier, from open file
io = open('copy_from.txt', 'wr')
data = ['Tom\tJenkins\t37\n',
'Madonna\tNULL\t45\n',
'Federico\tDi Gregorio\tNULL\n']
io.writelines(data)
io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy', null='NULL')
2017-11-28 11:30:15 +03:00
print("3) Copy %d records from file object using null = NULL" % len(data))
2005-03-01 19:41:02 +03:00
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
2017-11-28 11:30:15 +03:00
print(" Select using cursor returned %d rows" % len(rows))
2005-03-01 19:41:02 +03:00
for r in rows:
2017-11-28 11:30:15 +03:00
print(" %s %s\t%s" % (r[0], r[1], r[2]))
2005-03-01 19:41:02 +03:00
curs.execute("delete from test_copy")
conn.commit()
# copy_from using custom separator and null identifier
io = open('copy_from.txt', 'wr')
data = ['Tom:Jenkins:37\n', 'Madonna:NULL:45\n', 'Federico:Di Gregorio:NULL\n']
io.writelines(data)
io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy', ':', 'NULL')
2017-11-28 11:30:15 +03:00
print("4) Copy %d records from file object " % len(data) +
"using sep = : and null = NULL")
2005-03-01 19:41:02 +03:00
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
2017-11-28 11:30:15 +03:00
print(" Select using cursor returned %d rows" % len(rows))
2005-03-01 19:41:02 +03:00
for r in rows:
2017-11-28 11:30:15 +03:00
print(" %s %s\t%s" % (r[0], r[1], r[2]))
2005-03-01 19:41:02 +03:00
curs.execute("delete from test_copy")
conn.commit()
# anything can be used as a file if it has .read() and .readline() methods
data = StringIO.StringIO()
data.write('\n'.join(['Tom\tJenkins\t37',
'Madonna\t\N\t45',
'Federico\tDi Gregorio\t\N']))
data.seek(0)
curs.copy_from(data, 'test_copy')
2017-11-28 11:30:15 +03:00
print("5) Copy 3 records from StringIO object using defaults")
2005-03-01 19:41:02 +03:00
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
2017-11-28 11:30:15 +03:00
print(" Select using cursor returned %d rows" % len(rows))
2005-03-01 19:41:02 +03:00
for r in rows:
2017-11-28 11:30:15 +03:00
print(" %s %s\t%s" % (r[0], r[1], r[2]))
2005-03-01 19:41:02 +03:00
curs.execute("delete from test_copy")
conn.commit()
# simple error test
2017-11-28 11:30:15 +03:00
print("6) About to raise an error")
2005-03-01 19:41:02 +03:00
data = StringIO.StringIO()
data.write('\n'.join(['Tom\tJenkins\t37',
'Madonna\t\N\t45',
'Federico\tDi Gregorio\taaa']))
data.seek(0)
try:
curs.copy_from(data, 'test_copy')
2017-11-28 11:30:15 +03:00
except StandardError as err:
2005-03-01 19:41:02 +03:00
conn.rollback()
2017-11-28 11:30:15 +03:00
print(" Caught error (as expected):\n", err)
2005-03-01 19:41:02 +03:00
conn.rollback()
curs.execute("DROP TABLE test_copy")
os.unlink('copy_from.txt')
conn.commit()