-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
44 lines (37 loc) · 1.37 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
import mysql.connector
from mysql.connector import errorcode
from datetime import date, datetime, timedelta
# tutorial and error handling: https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
class Database:
def __init__(self, user, password, host = '127.0.0.1', database = 'workout'):
self.user = user
self.password = password
self.host = host
self.database = database
self.cnx = None
try:
self.cnx = mysql.connector.connect(user=user, password=password, host=host, database=database)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("user or password error")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
def add_time_now(self, name):
cursor = self.cnx.cursor()
date = datetime.now()
add_time = ("INSERT INTO info_time "
"(name, time)"
"VALUES (%s, %s)")
cursor.execute(add_time,(name, date))
self.cnx.commit()
cursor.close()
def close(self):
self.cnx.close()
def main():
print('----database main----')
db = Database('python','Hinoob22')
db.add_time_now('matt')
if __name__ == '__main__':
main()