Skip to content

Commit

Permalink
changing connection method
Browse files Browse the repository at this point in the history
  • Loading branch information
gustuxd committed Oct 18, 2020
1 parent 21cfdca commit f16f6c3
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@
import Result
import os

database_host = os.environ['MYSQL_TCC_HOST']
database_name = os.environ['MYSQL_TCC_NAME']
database_user = os.environ['MYSQL_TCC_USER']
database_pass = os.environ['MYSQL_TCC_PASS']

def get_connection():
mydb = mysql.connector.connect(
host="localhost",
user=os.environ['MYSQL_ROOT_USER'],
password=os.environ['MYSQL_ROOT_PASS'],
database="db_sentimentalizer"
host=database_host,
user=database_user,
password=database_pass,
database=database_name
)

return mydb

def tweet_insertion(tweet):

mydb = get_connection()

mycursor = mydb.cursor()

sql = "INSERT INTO tweets (id, username, text, sentiment, fav_count, retweet_count, created_at) VALUES (%s, %s, %s, %s, %s, %s, %s)"
#val = (tweet.)
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

return

def batch_tweet_insertion(list_of_tweets):

mydb = get_connection()
Expand All @@ -51,14 +40,14 @@ def batch_tweet_insertion(list_of_tweets):

def create_database():
mydb = mysql.connector.connect(
host="localhost",
user=os.environ['MYSQL_ROOT_USER'],
password=os.environ['MYSQL_ROOT_PASS']
host=database_host,
user=database_user,
password=database_pass
)

mycursor = mydb.cursor()

sql = "CREATE DATABASE IF NOT EXISTS db_sentimentalizer"
sql = "CREATE DATABASE IF NOT EXISTS " + database_name
mycursor.execute(sql)
mydb.commit()
mydb.close()
Expand Down

0 comments on commit f16f6c3

Please sign in to comment.