This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb.py
73 lines (52 loc) · 2.41 KB
/
db.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
# -*- coding: utf-8 -*-
# databse actions
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import json
import pymysql
import config
import utils
class DB(object):
"""docstring for DB"""
def __init__(self, **kwargs):
super(DB, self).__init__()
db_config = kwargs.get('db_config')
self._db = utils.merge(db_config, config.db_config)
print(self._db)
def store_gzh_list(self, gzhList, **kwargs):
if(type(gzhList) != list):
return False
table = kwargs.get('table', 'gzh')
[self._store_gzh_info(i, table=table) for i in gzhList]
def _store_gzh_info(self, info, **kwargs):
table = kwargs.get('table')
query = 'insert into ' + table + ' (`title`, `wechatid`, `avatar`, `qrcode`, `introduction`, `verification`) values ("' + info.get('title', '') + '", "' + info.get('wechatid', '') + '", "' + info.get('avatar', '') + '", "' + info.get('qrcode', '') + '", "' + info.get('introduction', '') + '", "' + info.get('verification', '') + '")'
self._execute(query)
def store_article(self, article, **kwargs):
for i in article:
article[i] = self._escape(article[i])
table = kwargs.get('table', 'articles')
query = 'insert into ' + table + ' (`title`, `poster`, `authorId`, `authorAvatar`, `authorName`, `col`, `description`, `content`, `updateTime`, `tag`, `likes`, `type`) values ("' + article.get('title', '') + '", "' + article.get('poster', '') + '", NULL, "' + article.get('authorAvatar', '') + '", "' + article.get('authorName', '') + '", "' + article.get('col', '') + '", "' + article.get('description', '') + '", "' + article.get('content', '').strip() + '", DATE_ADD(\'1970-01-01 00:00:00\', INTERVAL ' + str(article.get('updateTime', '')) + ' SECOND), 0, 0, 0)'
self._execute(query)
print(article['title'] + ' inserted')
def check_exist(self, title, **kwargs):
print('checking exist')
query = 'select * from ' + kwargs.get('table', 'articles') + ' where title="' + self._escape(title) + '"'
effect_rows = self._execute(query)
return effect_rows
def _execute(self, query):
conn = pymysql.connect(**self._db)
cursor = conn.cursor()
effect_rows = cursor.execute(query)
conn.commit()
cursor.close()
conn.close()
print('connection closed')
return effect_rows
def _escape(self, string):
if(type(string) == list):
return ','.join(string)
if(type(string) == int):
return str(string)
return string.replace('&', '&').replace('"', '"').replace("'", ''')