forked from Abdisalan/blog-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.py
40 lines (33 loc) · 1.08 KB
/
timeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import psycopg2
try:
connection = psycopg2.connect(user = "Abdi",
password = "",
host = "localhost",
port = "5432",
database = "twitter")
cursor = connection.cursor()
cursor.execute('''
SELECT u.name FROM user_follower as uf
JOIN users as u ON u.id = uf.follower_id
WHERE uf.user_id = 11;
''')
print("Abdi's followers:")
users = cursor.fetchall()
for name in users:
print(name[0])
cursor.execute('''
SELECT users.name, tweet.content FROM tweet
JOIN users ON users.id = tweet.user_id
ORDER BY tweet.content;
''')
tweets = cursor.fetchall()
for name, tweet in tweets:
print("%s: %s" % (name, tweet))
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")