Linkage of Class 3 databases: MySQL, MongoDB, Redis
From pymysql import *from pymongo import *from redis import *class MySQL (object): Def __init__ (self): Self.con = Connect (host= ' localhost ', port=3306, database= ' stu_info ', user= ' root ', password= ' MySQL ', charset= ' utf8 ') self.cur = Self.con.cursor () Self.flag = ["MySQL", None] def close (SE LF): Self.cur.close () self.con.close () def check (self, sid): Try:cur = Self.cur param = [SID] Sel_sql = "SELECT name from student where id =%s" cur.execute (Sel_sql, param) res = Cur.fetchone () if res:self.flag[1] = res[0] except Exception: Pass Finally:self.close () return Self.flagclass Mongo (object): Def __init__ (self): Self.client = mongoclient (host= ' localhost ', port=27017) self.db = Self.client.stu_info Self.col = SELF.DB.S Tudent Self.flag= ["Mongo", None] def check (self, sid): Try:res = Self.col.find_one ({"id": SID}) If res: SELF.FLAG[1] = res["name"] except Exception:pass finally:return self. Flagclass Redis (object): Def __init__ (self): Self.client = Strictredis () Self.flag = ["Redis", None] D EF check (self, sid): Try:res = self.client.get (SID) if res:self.flag[1] = RE S.decode () except Exception:pass Finally:return self.flagclass Check (object): Def __init__ (self): self.msg = ["Not in Library", "The Student not Found"] def check (self, sid): Redis = Redis () self.msg = RE Dis.check (SID) if SELF.MSG[1]: return self.msg Else:mongo = MONGO () self.m sg = mongo.check (SID) if SELF.MSG[1]: return self.msg else:mysql = My SQL () Self.msg = Mysql.check (SID) if SELF.MSG[1]: return self.msg Else: Self.msg = ["Not in Library", "The Student not Found"] return Self.msgdef Main (): stu_id = input ("Please enter ID number to query:") c heck_id = Check () id_msg = Check_id.check (stu_id) print (id_msg) if __name__ = = ' __main__ ': Main ()
Linkage of Class 3 databases: MySQL, MongoDB, Redis