forked from feilongwang92/mining-app-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
56 lines (45 loc) · 1.6 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
#import mysql.connector
#from mysql.connector import Error
import pymysql as mysql
def mysql_read_connection(database):
conn = mysql.connect(host='localhost',
database=database,
user='root',
password='Wfl920310')
return conn
def execute(database, query):
try:
connection = mysql_read_connection(database)
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
except Exception as e:
print('Error:', e)
finally:
cursor.close()
connection.close()
def fetch_results(database, query, single_row=False, single_col=False):
connection = mysql_read_connection(database)
cursor = connection.cursor()
cursor.execute(query)
if single_row:
result = cursor.fetchone()
if single_col:
result = result[0] if result else result
else:
result = list(cursor.fetchall())
if single_col:
result = [item[0] for item in result]
return result
# id = 'a0be48f6b592ebcbd536f6a615be005f2066335665973d868907bd32706fe81c'
# acc = 21
# query = """
# SELECT * from `testtest_mysql1` where id='{id}' and accuracy='{acc}'
# """.format(id=id, acc=acc)
#
# execute(query)
# results = fetch_results(query)
# print (results)
# dbcursor = mysql.connect(host='localhost', database='cuebiq201911', user='root', password='Wfl920310').cursor()
# dbcursor.execute(query)
# results = list(dbcursor.fetchall())