Last active
August 15, 2020 17:45
-
-
Save aniruddha27/cd5764bbac6c8ec29ff3bf7a9675e223 to your computer and use it in GitHub Desktop.
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
# Insert Tweet data into database | |
def dbConnect(user_id, user_name, tweet_id, tweet, retweet_count, hashtags): | |
conn = psycopg2.connect(host="localhost",database="TwitterDB",port=5432,user=<user>,password=<password>) | |
cur = conn.cursor() | |
# insert user information | |
command = '''INSERT INTO TwitterUser (user_id, user_name) VALUES (%s,%s) ON CONFLICT | |
(User_Id) DO NOTHING;''' | |
cur.execute(command,(user_id,user_name)) | |
# insert tweet information | |
command = '''INSERT INTO TwitterTweet (tweet_id, user_id, tweet, retweet_count) VALUES (%s,%s,%s,%s);''' | |
cur.execute(command,(tweet_id, user_id, tweet, retweet_count)) | |
# insert entity information | |
for i in range(len(hashtags)): | |
hashtag = hashtags[i] | |
command = '''INSERT INTO TwitterEntity (tweet_id, hashtag) VALUES (%s,%s);''' | |
cur.execute(command,(tweet_id, hashtag)) | |
# Commit changes | |
conn.commit() | |
# Disconnect | |
cur.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment