forked from DouglasCorreiaBrito/tcc-fateczl-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_utils.py
108 lines (84 loc) · 2.38 KB
/
db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import mysql.connector
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=database_host,
user=database_user,
password=database_pass,
database=database_name
)
return mydb
def batch_tweet_insertion(list_of_tweets):
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 = []
for tweet in list_of_tweets:
val.append(tweet.split_values())
mycursor.executemany(sql, val)
mydb.commit()
mydb.close()
print(mycursor.rowcount, "was inserted.")
return
def execute(sql, values=None):
mydb = get_connection()
mycursor = mydb.cursor()
mycursor.execute(sql, values)
mydb.commit()
mydb.close()
return
def execute_many(sql, values=None):
mydb = get_connection()
mycursor = mydb.cursor()
mycursor.executemany(sql, values)
mydb.commit()
mydb.close()
return
def create_database():
mydb = mysql.connector.connect(
host=database_host,
user=database_user,
password=database_pass
)
mycursor = mydb.cursor()
sql = "CREATE DATABASE IF NOT EXISTS " + database_name
mycursor.execute(sql)
mydb.commit()
mydb.close()
return
def create_table_tweets():
mydb = get_connection()
mycursor = mydb.cursor()
sql = """CREATE TABLE IF NOT EXISTS tweets(
id varchar(50)
,username varchar(50)
,text text
,sentiment varchar(50)
,fav_count varchar(50)
,retweet_count varchar(50)
,created_at varchar(50)
)"""
mycursor.execute(sql)
mydb.commit()
mydb.close()
return
def create_table_search_terms():
mydb = get_connection()
mycursor = mydb.cursor()
sql = """CREATE TABLE IF NOT EXISTS search_terms(
term varchar(100) primary key,
search_qty int
)"""
mycursor.execute(sql)
mydb.commit()
mydb.close()
return
def initialize_database():
create_database()
create_table_tweets()
create_table_search_terms()