-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_connector.py
53 lines (45 loc) · 1.43 KB
/
db_connector.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
import pymysql
from datetime import datetime
def create_connection():
return pymysql.connect(
host='127.0.0.1',
port=3306,
user='user',
password='password',
db='mydb',
cursorclass=pymysql.cursors.DictCursor
)
def create_user(user_id, user_name):
conn = create_connection()
try:
with conn.cursor() as cursor:
cursor.execute("INSERT INTO users (user_id, user_name, creation_date) VALUES (%s, %s, %s)",
(user_id, user_name, datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
conn.commit()
finally:
conn.close()
def get_user(user_id):
conn = create_connection()
try:
with conn.cursor() as cursor:
cursor.execute("SELECT user_name FROM users WHERE user_id=%s", (user_id,))
result = cursor.fetchone()
return result['user_name']
finally:
conn.close()
def update_user(user_name, user_id):
conn = create_connection()
try:
with conn.cursor() as cursor:
cursor.execute("UPDATE users SET user_name=%s WHERE user_id=%s", (user_name, user_id))
conn.commit()
finally:
conn.close()
def delete_user(user_id):
conn = create_connection()
try:
with conn.cursor() as cursor:
cursor.execute("DELETE FROM users WHERE user_id=%s", (user_id,))
conn.commit()
finally:
conn.close()