-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadd_db.py
33 lines (29 loc) · 996 Bytes
/
add_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
import pymysql
class Flag:
def __init__(self, db_name):
self.db = self.connect_to_db(db_name)
def connect_to_db(self, db_name):
db = pymysql.connect(host='localhost',user='root',password='qwe1234..',port=3306,db=db_name)
return db
def close_db(self):
self.db.close()
def clear_table(self):
cursor = self.db.cursor()
sql3 = 'DELETE FROM users'
try:
cursor.execute(sql3)
self.db.commit()
print('clear table ok')
except Exception as e:
self.db.rollback()
print('clear table error: ', e)
def add(self,data):
cursor = self.db.cursor()
sql3 = 'INSERT INTO user(message, username) values(%s, %s)'
try:
cursor.execute(sql3, (data['message'], data['username']))
self.db.commit()
print('insert ok')
except Exception as e:
self.db.rollback()
print('insert error: ',e)