from pymongo import MongoClient
class User:
FirstName = None
LastName = None
Email = None
Password = None
def __init__(self,Email,Password,FirstName,LastName = ""):
self.Email = Email
self.Password = Password
self.FirstName = FirstName
self.LastName = LastName
def json(self):
json = {
"Email":self.Email,
"Password":self.Password,
"FirstName":self.FirstName,
"LastName":self.LastName
}
return json
class GameDB:
url = ""
client = None
db = None
def __init__(self,url = "mongodb://jo:jokda@localhost"):
self.url = url
self.client = MongoClient(self.url)
self.db = self.client.game
def insert(self,user,flag=False):
if flag:
self.db.inventory.insert_many(user)
return 1
select = self.select({"Email":user.Email})
if len(select) != 0 :
return -1
json_user = user.json()
self.db.inventory.insert_one(json_user)
return 1
def select(self,condition={}):
cursor = self.db.inventory.find(condition)
result = [doc for doc in cursor]
return result
def update(self,user:User,flag=True):
select = self.select({"Email":user.Email})
if len(select)<1:
return -1
if flag :
self.db.inventory.update_many(
{"Email":user.Email},
{"$set":{
"Password":user.Password,
"FirstName":user.FirstName,
"LastName":user.LastName
}},
)
return 1
self.db.inventory.update_one(
{"Email":user.Email},
{"$set":{
"$Password":user.Password,
"$FirstName":user.FirstName,
"$LastName":user.LastName
}},
)
return 1
def delete(self,condition,flag=True):
if flag :
self.db.inventory.delete_many(condition)
else:
self.db.inventory.delete_one(condtion)
return 1
#user = User("19@qq.com","password","pa","zika")
#db = GameDB()
#db.add(user)
#db.printf()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)