-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBMgr.py
44 lines (37 loc) · 1.28 KB
/
DBMgr.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
import pymysql as my
class DBHelper:
def __init__(self):
self.db_init()
def db_init(self):
self.conn = my.connect(
host='localhost',
user='root',
password='thfxkfbb46',
db='pythonDB',
charset='utf8',
cursorclass=my.cursors.DictCursor )
def db_free(self):
if self.conn:
self.conn.close()
def db_selectKeyword(self):
rows = None
with self.conn.cursor() as cursor:
sql = "select * from tbl_keyword;"
cursor.execute(sql)
rows = cursor.fetchall()
print(rows)
return rows
def db_insertCrawlingData(self, title, price, area, contents, keyword ):
with self.conn.cursor() as cursor:
sql = '''
insert into `tbl_crawlingdata`
(title, price, area, contents, keyword)
values( %s,%s,%s,%s,%s )
'''
cursor.execute(sql, (title, price, area, contents, keyword) )
self.conn.commit()
if __name__=='__main__':
db = DBHelper()
print( db.db_selectKeyword() )
print( db.db_insertCrawlingData('1','2','3','4','5') )
db.db_free()