-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlOperations.py
95 lines (89 loc) · 3.26 KB
/
MysqlOperations.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
import pymysql.cursors
import yaml
class MysqlOperations:
def __init__(self):
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile)['mysql_login']
connection = pymysql.connect(host='localhost',
user='root',
password=cfg['password'],
db=cfg['db_name'],
charset=cfg['charset'],
cursorclass=pymysql.cursors.DictCursor)
self.connection = connection
def fetch_obs(self,sql_query):
cursor = self.connection.cursor()
cursor.execute(sql_query)
result = cursor.fetchall()
return result
def insert_entry_to_posts(self,table_name,entry):
'''
page_id varchar(50) not null,
post_id varchar(50) not null,
post_time datetime DEFAULT NULL,
post varchar(1024) not null,
post_url varchar(255) not null,
fb_post_url varchar(255) not null,
comments_numb int(4) default 0,
'''
sql = "INSERT INTO {} (page_id,post_id,post_time,post,post_url,fb_post_url,comments_numb)"\
"VALUES (%s,%s,%s,%s,%s,%s,%s)" . format(table_name)
cursor = self.connection.cursor()
try:
cursor.execute(sql, (entry['page_id'],entry['id'],\
entry['created_time'],entry['message'],entry['link'],entry['actions'][0]['link'],0))
except pymysql.err.IntegrityError:
print("post with id = {} is duplication in db" . format(entry['id']))
return "dublication"
except:
print("post with id = {} was not inserted due to unknown error" . format(entry['id']))
return 0
else:
self.connection.commit()
return "success"
def insert_entry_to_comments(self,table_name,entry):
'''
post_id varchar(50) not null,
comment_id varchar(50) not null,
commenter varchar(100) not null,
commenter_id varchar(50) not null,
comment_time datetime DEFAULT NULL,
comment varchar(1024) not null,
comment_count int(3) DEFAULT 0,
comment_likes int(5) DEFAULT 0,
app_name varchar(20) DEFAULT NULL,
app_id varchar(50) DEFAULT NULL,
'''
sql = "INSERT INTO comments (post_id,comment_id,commenter,commenter_id,comment_time," \
"comment,comment_count,comment_likes,app_name,app_id)"\
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" . format(table_name)
cursor = self.connection.cursor()
try:
cursor.execute(sql, (entry['post_id'],entry['id'],entry['from']['name'],entry['from']['id'],\
entry['created_time'],entry['message'],entry['comment_count'],entry['like_count'],\
entry['application']['namespace'],entry['application']['id']))
except pymysql.err.IntegrityError:
print("comment with id = {} is duplication in db" . format(entry['id']))
return "dublication"
except:
print("comment with id = {} was not inserted due to unknown error" . format(entry['id']))
return 0
else:
self.connection.commit()
return "success"
def update_table(self,table_name,s_key,s_val,w_key,w_value):
'''
update mysql table
'''
sql = "update {} set {} = {} where {} = '{}'".\
format(table_name,s_key,s_val,w_key,w_value)
cursor = self.connection.cursor()
try:
cursor.execute(sql)
except:
print("comment_numb update error")
else:
self.connection.commit()
print("{} comments where inserted successfully\n" . format(s_val))
if __name__ == "__main__":
pass