Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. conn = psycopg2.connect("dbname=postgres user=postgres password=psswd")
  2. cur = conn.cursor()
  3. cur.copy_to(sys.stdout,'mytable',sep = 't')
  4.  
  5. E0307 1 M 400 Ethan UTDallas 12.98580404 N 50.79403657 1
  6. E0307 1 M 400 Lucas Baylor 15.18511175 N 56.87285183 3
  7. E0307 1 M 400 Jackson Baylor 13.64228411 N 56.87285183 3
  8. E0307 1 M 400 Jacob Baylor 13.19878974 N 56.87285183 3
  9. E0307 1 M 400 Samuel Baylor 14.84666623 N 56.87285183 3
  10.  
  11. ProgrammingError: can't execute COPY TO: use the copy_to() method instead
  12.  
  13. import psycopg2 as pg
  14. import pandas as pd
  15. import pandas.io.sql as psql
  16.  
  17. connection = pg.connect("dbname=postgres user=postgres password=psswd")
  18. #my_table = pd.read_sql_table('table_name', connection)
  19. my_table = pd.read_sql('select * from my-table-name', connection)
  20. another_attempt= psql.read_sql("SELECT * FROM my-table-name", connection)
  21.  
  22. print(my_table)
  23.  
  24. # OR
  25. print(another_attempt)
  26.  
  27. copy = "copy mytable to stdout with csv header delimiter 't' null 'NULL'"
  28. cursor.copy_expert(copy, sys.stdout)
  29.  
  30. header = [i[0] for i in cur.description
  31. print header
  32. cur.copy_to(sys.stdout, 'table', sep='t', null='N')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement