Skip to content

Commit

Permalink
Merge pull request #14 from dcloud/master
Browse files Browse the repository at this point in the history
Use psycopg2's copy_expert so we can copy from a local file to a remote database.
  • Loading branch information
palewire committed Aug 25, 2015
2 parents 95a2530 + 41d5dfd commit dc0bfa6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 3 additions & 3 deletions postgres_copy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def save(self, silent=False, stream=sys.stdout):
# Run all of the raw SQL
cursor.execute(drop_sql)
cursor.execute(create_sql)
cursor.execute(copy_sql)
fp = open(self.csv_path, 'r')
cursor.copy_expert(copy_sql, fp)
cursor.execute(insert_sql)
cursor.execute(drop_sql)

Expand Down Expand Up @@ -151,12 +152,11 @@ def prep_copy(self):
"""
sql = """
COPY "%(db_table)s" (%(header_list)s)
FROM '%(csv_path)s'
FROM STDIN
WITH CSV HEADER %(extra_options)s;
"""
options = {
'db_table': self.temp_table_name,
'csv_path': self.csv_path,
'extra_options': '',
'header_list': ", ".join([
'"%s"' % h for f, h in self.field_header_crosswalk
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def run(self):
url='http://www.github.com/california-civic-data-coalition/django-postgresql-copy/',
license="MIT",
packages=("postgres_copy",),
install_requires=(
'psycopg2>=2.5',
),
cmdclass={'test': TestCommand},
classifiers=[
'Programming Language :: Python',
Expand Down

0 comments on commit dc0bfa6

Please sign in to comment.