Upload database.py
Browse files- database.py +35 -0
database.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from deta import Deta # pip3 install deta
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv ## pip3 install python-dotenv
|
4 |
+
|
5 |
+
DETA_KEY = "c0zegv33efm_4MBTaoQAn76GzUfsZeKV64Uh9qMY3WZb"
|
6 |
+
# load_dotenv(".env")
|
7 |
+
# DETA_KEY = os.getenv("DETA_KEY")
|
8 |
+
# print(DETA_KEY)
|
9 |
+
|
10 |
+
deta = Deta(DETA_KEY)
|
11 |
+
|
12 |
+
# mybase is the name of the database in Deta. You can change it to any name you want.
|
13 |
+
db = deta.Base("mybase")
|
14 |
+
# print(db)
|
15 |
+
|
16 |
+
#* 目前我的mybase中有一個key是一个临时字符串。
|
17 |
+
def insert_user(username, name, password):
|
18 |
+
return db.put({"key": username, "name": name, "password": password}) ## username is the key, a unique identifier.
|
19 |
+
|
20 |
+
def fetch_all_users():
|
21 |
+
res = db.fetch()
|
22 |
+
return res.items
|
23 |
+
|
24 |
+
all_users = fetch_all_users()
|
25 |
+
print(all_users)
|
26 |
+
|
27 |
+
def get_user(username):
|
28 |
+
return db.get(username)
|
29 |
+
|
30 |
+
def update_user(username, name, password):
|
31 |
+
return db.put({"key": username, "name": name, "password": password})
|
32 |
+
|
33 |
+
def delete_user(username):
|
34 |
+
return db.delete(username)
|
35 |
+
|