Created
November 1, 2016 23:08
-
-
Save buckbaskin/498c54d8ac13f687f7bdc5b14eb50df2 to your computer and use it in GitHub Desktop.
A simple example of using Postgres in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psycopg2 | |
# make a connection | |
try: | |
conn = psycopg2.connect(database='postgres', user='postgres', password='economicalchinchillacorndog', host='localhost') | |
cur = conn.cursor() | |
except: | |
print('Database Connection Failed') | |
raise | |
# run a SQL query | |
cur.execute('SELECT * FROM orders') | |
# iterate through the results | |
for item in cur.fetchall(): | |
print(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment