Skip to content

Instantly share code, notes, and snippets.

@aniruddha27
Last active August 15, 2020 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniruddha27/cd5764bbac6c8ec29ff3bf7a9675e223 to your computer and use it in GitHub Desktop.
Save aniruddha27/cd5764bbac6c8ec29ff3bf7a9675e223 to your computer and use it in GitHub Desktop.
# 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