-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.py
53 lines (42 loc) · 1.37 KB
/
mysql.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
import pymysql.cursors
import os
import json
import base64
import datetime
import random
import os
import sys
# Connect to the database
def query_db(sql_syntax,target,db):
try:
if target=='test':
connection = pymysql.connect(host='1',
user='r',
password='z1',
db=db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
else:
connection = pymysql.connect(host='17',
user='r',
password='q3',
db=db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
# Create a new record
sql = sql_syntax
cursor.execute(sql)
connection.commit()
#print(cursor.description)
rows=[]
for row in cursor:
rows.append(row)
return rows
connection.commit()
except Exception as e:
print (e)
rows="no such row"
finally:
return rows
connection.close()