-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
49 lines (41 loc) · 1.32 KB
/
database.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
import pymysql
import pymysql.cursors
### super secret credentials ###
host = 'localhost'
user = 'root'
password = '199072zhuma'
db = 'lumberjack'
charset = 'utf8mb4'
conn = pymysql.connect(host=host,user=user,password=password,db=db,charset=charset,cursorclass=pymysql.cursors.DictCursor)
db_cursor = conn.cursor()
def get_all_users():
sql = "SELECT * FROM `lj_users`;"
db_cursor.execute(sql)
result = db_cursor.fetchall()
return result
result = db_cursor.fetchone()
print(result)
def get_user_by_id(id):
sql = "SELECT * FROM `lj_users` WHERE id = {};".format(id)
db_cursor.execute(sql)
result = db_cursor.fetchone()
print(result)
def create_user(user):
try:
with conn.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `lj_users` (`name`, `email`, `password`) VALUES (%s, %s, %s)"
cursor.execute(sql, (user.name, user.email, user.password))
cursor.execute(sql, (user.name, name.email, name.password))
# conn is not autocommit by default. So you must commit to save
# your changes.
conn.commit()
with conn.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `lj_users` WHERE `email` = %s"
sql = "SELECT `id`, `password` FROM `lj_users` WHERE `email`=%s"
cursor.execute(sql, (user.email))
result = cursor.fetchone()
print(result)
finally:
conn.close()