Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def store_tweets_in_table(term_to_search, user_id, created_at, tweet, user_name, retweetcount):
- """
- This function open a connection with an already created database and inserts into corresponding table
- tweets related to the selected topic
- """
- #Connect to Twitter Database created in Postgres
- conn_twitter = psycopg2.connect(dbname=dbnametwitter, user=usertwitter, password=passwordtwitter, host=hosttwitter, port=porttwitter)
- #Create a cursor to perform database operations
- cursor_twitter = conn_twitter.cursor()
- #with the cursor now, insert tweet into table
- cursor_twitter.execute("INSERT INTO twitter_users (user_id, user_name) VALUES (%s, %s) ON CONFLICT(user_id) DO NOTHING;", (user_id, user_name))
- cursor_twitter.execute("INSERT INTO %s (created_at, tweet, user_id, retweetcount) VALUES (%%s, %%s, %%s, %%s);" %('tweets_'+term_to_search),
- (created_at, tweet, user_id, retweetcount))
- #Commit changes
- conn_twitter.commit()
- #Close cursor and the connection
- cursor_twitter.close()
- conn_twitter.close()
- return
Add Comment
Please, Sign In to add comment