-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_management.py
70 lines (54 loc) · 2.91 KB
/
data_management.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
57
58
59
60
61
62
63
64
65
66
67
68
69
import sqlite3
import datetime
import tkinter as tk
import hashlib
def encrypt(raw_text):
"""encrypt user password using sha256 algorithm"""
s = hashlib.sha256()
s.update(raw_text.encode('utf8'))
return s.hexdigest()
class Data_base(object):
def __init__(self, name):
""" initialize a database """
self.record_name = name
self.connection = sqlite3.connect(f"{self.record_name}.db")
self.cursor = self.connection.cursor()
self.cursor.execute("""CREATE TABLE IF NOT EXISTS balance_sheet (id integer PRIMARY KEY AUTOINCREMENT,
user_name text NOT NULL,
expense integer,
revenue integer,
time text NOT NULL
)""")
self.cursor.execute("""CREATE TABLE IF NOT EXISTS users (uid integer PRIMARY KEY AUTOINCREMENT,
user_name text,
pass_word_hash text,
balance integer)""")
self.connection.commit()
def update_data(self, table_name, column_id, value):
self.cursor.execute("UPDATE {} SET {} = ? WHERE {} = {}", (column_name, value))
self.connection.commit()
def insert_data(self, table_name, values):
if table_name == "balance_sheet":
self.cursor.execute("INSERT INTO balance_sheet (user_name, expense, revenue, time) VALUES (?, ?, ?, ?)", (user_name, values[0], values[1], str(datetime.datetime.today)))
self.connection.commit()
elif table_name == "users":
self.cursor.execute("INSERT INTO users (user_name, pass_word_hash, balance) VALUES (?, ?, ?)", (values[0], encrypt(values[1]), values[2]))
self.connection.commit()
else:
raise ValueError("table name does not exist")
def delete_data(self, value):
self.cursor.execute("DELETE FROM balance_sheet WHERE id = ?", (value,))
self.connection.commit()
def get_name(self, name):
self.cursor.execute("SELECT user_name FROM users WHERE user_name = ?", (name,))
self.connection.commit()
result = self.cursor.fetchone()
return result[0] if result else None
def get_pswd(self, user_name):
self.cursor.execute("SELECT pass_word_hash FROM users WHERE user_name = ?", (user_name,))
self.connection.commit()
return self.cursor.fetchone()[0]
def get_balance(self, current_user):
self.cursor.execute("SELECT balance FROM users WHERE user_name = ?", (current_user,))
self.connection.commit()
return self.cursor.fetchone()[0]