Advertisement
Guest User

psycopg2 with Pandas

a guest
Mar 13th, 2013
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. # http://stackoverflow.com/questions/1466741/parameterized-queries-with-psycopg2-python-db-api-and-postgresql
  2.  
  3. import psycopg2
  4. import pandas.io.sql as sqlio
  5.  
  6. username = 'yomama'
  7. pwd = 'yopwd'
  8.  
  9. conn = psycopg2.connect("host='your_host' dbname='mydb' user=" + username + " password=" + pwd)
  10.  
  11. sql = "select * from medicine_journal where medication = '%(medicine)s'"
  12.  
  13. df = sqlio.read_frame(sql % {'medicine':'flonase'}, conn) # Create a dataframe that consists of the data defined by our SQL
  14.  
  15. conn.close() # We've created our dataframe, so we can discard our connection now...
  16.  
  17. print df.to_string()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement