Guest User

Untitled

a guest
Dec 26th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. def store_tweets_in_table(term_to_search, user_id, created_at, tweet, user_name, retweetcount):
  2. """
  3. This function open a connection with an already created database and inserts into corresponding table
  4. tweets related to the selected topic
  5. """
  6.  
  7. #Connect to Twitter Database created in Postgres
  8. conn_twitter = psycopg2.connect(dbname=dbnametwitter, user=usertwitter, password=passwordtwitter, host=hosttwitter, port=porttwitter)
  9.  
  10. #Create a cursor to perform database operations
  11. cursor_twitter = conn_twitter.cursor()
  12.  
  13. #with the cursor now, insert tweet into table
  14. cursor_twitter.execute("INSERT INTO twitter_users (user_id, user_name) VALUES (%s, %s) ON CONFLICT(user_id) DO NOTHING;", (user_id, user_name))
  15.  
  16. cursor_twitter.execute("INSERT INTO %s (created_at, tweet, user_id, retweetcount) VALUES (%%s, %%s, %%s, %%s);" %('tweets_'+term_to_search),
  17. (created_at, tweet, user_id, retweetcount))
  18.  
  19. #Commit changes
  20. conn_twitter.commit()
  21.  
  22. #Close cursor and the connection
  23. cursor_twitter.close()
  24. conn_twitter.close()
  25. return
Add Comment
Please, Sign In to add comment