This repository was archived by the owner on Feb 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMySQL.py
More file actions
71 lines (60 loc) · 1.73 KB
/
Copy pathMySQL.py
File metadata and controls
71 lines (60 loc) · 1.73 KB
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
from config import *
import pymysql
from pymysql.err import MySQLError
class MySQL():
"""
The abstraction of operations to MySQL database.
"""
def __init__(self):
self.conn = None
def connect(self):
"""
Connect to the database.
"""
self.conn=pymysql.connect(host=DB_HOST, port=DB_PORT, user=DB_USER, password=DB_PASSWD,database=DB_NAME)
def close(self):
"""
Close the connection.
"""
self.conn.close()
def select(self,query):
"""
Do SELECT query.
:param query: The SQL query to execute.
:return: The result of executing the query.
"""
cursor = self.conn.cursor()
cursor.execute(query)
return cursor.fetchall()
def insert(self,query,data : tuple):
"""
Deal with the INSERT/UPDATE/DELETE query.
:param query: The query to execute.
:param data: The data to include in the query.
"""
try:
cursor = self.conn.cursor()
cursor.execute(query,data)
self.conn.commit()
except MySQLError as e:
print(e)
self.conn.rollback()
def insertMany(self,query,data):
"""
Insert a list of data to the database.
:param query: The query command.
:param data: The data to include in the query.
"""
try:
cursor = self.conn.cursor()
cursor.executemany(query,data)
self.conn.commit()
except MySQLError as e:
print(e)
self.conn.rollback()
if __name__ == '__main__':
db = MySQL()
db.connect()
ret = db.select("""SELECT * FROM softwares""")
print(ret)
db.close()