Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_tweets_table(term_to_search):
- """
- This function open a connection with an already created database and creates a new table to
- store tweets related to a subject specified by the user
- """
- #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, create two tables, users twitter and the corresponding table according to the selected topic
- cursor_twitter.execute("CREATE TABLE IF NOT EXISTS twitter_users (user_id VARCHAR PRIMARY KEY, user_name VARCHAR);")
- query_create = "CREATE TABLE IF NOT EXISTS %s (id SERIAL, created_at timestamp, tweet text NOT NULL, user_id VARCHAR, retweetcount int, PRIMARY KEY(id), FOREIGN KEY(user_id) REFERENCES twitter_users(user_id));" %("tweets_"+term_to_search)
- cursor_twitter.execute(query_create)
- #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