forked from tweepy/tweepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_table_creation_sql.py
37 lines (33 loc) · 1.43 KB
/
2_table_creation_sql.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
# Table creation
commands = (# Table 1
'''Create Table TwitterUser(User_Id BIGINT PRIMARY KEY, User_Name TEXT);''',
# Table 2
'''Create Table TwitterTweet(Tweet_Id BIGINT PRIMARY KEY,
User_Id BIGINT,
Tweet TEXT,
Retweet_Count INT,
CONSTRAINT fk_user
FOREIGN KEY(User_Id)
REFERENCES TwitterUser(User_Id));''',
# Table 3
'''Create Table TwitterEntity(Id SERIAL PRIMARY KEY,
Tweet_Id BIGINT,
Hashtag TEXT,
CONSTRAINT fk_user
FOREIGN KEY(Tweet_Id)
REFERENCES TwitterTweet(Tweet_Id));''')
import psycopg2
# Connection to database server
conn = psycopg2.connect(host="localhost",database="TwitterDB",port=5432,user='postgres',password='790213Aa')
# Create cursor to execute SQL commands
cur = conn.cursor()
# Execute SQL commands
i=0
for command in commands:
# Create tables
cur.execute(command)
# print(f'Table {i} is created successfully.')
# Close communication with server
conn.commit()
cur.close()
conn.close()